一.Collection(java.util)

1.概述:具有相同性质的一类事物的汇聚的整体,称为集合.任何集合都包含三块内容:对外的接口/接口的实现/对集合运算的算法.
         java中使用Collection来表示单列集合顶层的接口.
         public interface Collection<E> extends Itretaor<E>{}
         注意:接口不能直接实例化,得需要其子类

2.特点及体系:Collection为顶层接口,仅描述集合的共性内容.常用的子接口为 List 和 Set.
     共性内容:
         (1)集合容器的的长度可以改变;
         (2)存储的是同一个类型的数据;
         (3)只能存引用数据类型(基本数据类型需进行装箱操作);
     List : 有序/允许重复;
     Set : 无序/不允许重复;
     Queue:队列接口;
     SortedSet:对集合中的数据进行排序;

3.常用方法:
     public boolean add(E e){}:向集合中插入对象;
     public boolean addAll(Collection<? extends E> c){}:插入一个集合的内容;
     public void clear(){}:清除此集合中的所有内容,但是保留该集合容器;
     public boolean contains(Object o){}:判定某一对象是否存在该集合中;
     public boolean containsAll(Collection<?> c){}:判断一组对象是否存在该集合中;
     public boolean equals(Object o){}:对象比较;
     public int hashCode(){}:哈希值;
     public boolean isEmpty(){}:判断集合是否为空;
     public Iterator<E> iterator(){}:为Iterator接口实例化;返回迭代器实例化对象;
     public boolean remove(Object o){}:删除指定对象;
     public boolean removeAll(Collection<?> c){}:删除一组对象;
     public boolean retainAll(Collection<?> c){}:保存指定内容;
     public int size(){}:求出集合大小;
     public Object[] toArray(){}:将一个集合变为对象数组;

代码:

  1 import java.util.Collection;
2 import java.util.ArrayList;
3
4 public class Coll{
5 public static void main(String[] args){
6 //通过子类实例化接口对象,因为接口全为抽象无法直接实例化
7 Collection<String> c = new ArrayList<String>();
8 //添加元素add()
9 c.add("Rose");
10 c.add("Jack");
11 //查看集合内容
12 System.out.println(c);//[Rose, Jack]
13 //查看集合长度
14 System.out.println(c.size());//2
15 //查看是否包含tom
16 System.out.println(c.contains("tom"));//false
17 //查看是否包含Tom
18 c.add("Tom");
19 System.out.println(c.contains(new String("Tom")));//true--new String("Tom")返回一个"Tom"字符串
20 //删除指定对象remove()
21 c.remove("Rose");
22 System.out.println(c);//[Jack, Tom]
23 //删除所有对象-保留集合
24 c.clear();
25 System.out.println(c);//[]
26 //判断是否为空
27 System.out.println(c.isEmpty());//true
28 }
29 }

二.Iterator(java.util)

1.定义:专门操作集合的工具类,只要碰到集合的输出操作就一定使用Iterator接口
     pbulic interface Iterator<E>

2.常用方法:
     public boolean hasNext(){}:判断是否存在下一个值;
     public E next(){}:取出当前元素;
     public void remove(){}:移除当前元素;

3.注意事项:

(1)使用迭代器只能删除数据,不能添加数据;

(2)迭代器迭代数据的过程中,不能使用集合自带的方法,改变集合的长度!否则会报异常:ConcurrentModificationException 并发修改异常!

(3)注意在构建迭代器的时候,其后<>内指定类型(如:Iterator<String> it = Collection.iterator();),否则运算会出错,仅打印没问题;

  1 代码1://需求:利用Iterator输出集合内容
2 import java.util.Collection;
3 import java.util.ArrayList;
4 import java.util.Iterator;
5
6 public class IteratorDemo{
7 public static void main(String[] args){
8 //通过子类实例化对象实例化Collection对象
9 Collection<String> c = new ArrayList<String>();
10 //向集合c中添加元素
11 c.add("Green");
12 c.add("Smith");
13 c.add("Philip");
14 //通过Collection的iterator方法,创建iterator对象
15 Iterator<String> it =c.iterator(); //注意在构建迭代器的时候,指定类型(此处为<String>),否则运算会出错,仅打印没问题
16 //输出集合c中的所有元素
17 while(it.hasNext()){
18 System.out.print(it.next()+"\t");//注意,建议判断一次仅调用一次next()方法,由于next()在输出的时候也会使指针向前移动,容易发生"NoSuchElementException"
19 //System.out.println("i like"+it.next());//java.util.NoSuchElementException
20 }
21 }
22 }
  1 代码2://需求:删除元素
2 import java.util.Collection;
3 import java.util.ArrayList;
4 import java.util.Iterator;
5
6 public class IteratorDemo{
7 public static void main(String[] args){
8 //通过子类实例化对象实例化Collection对象
9 Collection<String> c = new ArrayList<String>();
10 //向集合c中添加元素
11 c.add("Green");
12 c.add("Smith8");
13 c.add("Philip");
14 //foreach迭代
15 for(String s : c){
16 System.out.print(s+"\t") //Green Smith8 Philip
17 }
18 System.out.println("====================")
19 //通过Collection的iterator方法,创建iterator对象
20 Iterator<String> it =c.iterator();//注意在构建迭代器的时候,指定类型(此处为<String>),否则运算会出错,仅打印没问题
21 //输出集合c中的所有元素
22 while(it.hasNext()){
23 //取出集合中的元素
24 String str = it.next();
25 //判断是否含有数字--正则(字符串最后一位是数字的)
26 if(str.matches("\\w+\\d")){
27 it.remove();
28 }else{
29 System.out.print(str+"\t");// Green Philip
30 }
31 }
32 System.out.print("删除带数字的名称后为:"+c+"\t");//删除带数字的名称后为:[Green, Philip]
33 }
34 }
35

三.for--each

1.作用:增强for是JDK1.5之后出现的新特性,可以用于迭代(遍历/循环)集合或数组!可以使用增强for替代迭代器,获取集合的元素内容!

2.格式:
     for(数据类型 变量名 : 对象名或数组名){
             集合或数组中的每一个元素!
         }

3.代码:见二.Iterator代码2

Collection-Iterator-foreach的更多相关文章

  1. 09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHashSet,TreeSet

    09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHas ...

  2. <JAVA8新增内容>关于集合的操作(Collection/Iterator/Stream)

    因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterato ...

  3. java 测试:iterator foreach for 三种迭代方式哪种更快?

    代码: public class main { public static void main(String[] p_args){ ArrayList<String> _l_string ...

  4. Collection Iterator 迭代器

    Collection c=new ArrayList(); c.add(123); //迭代器遍历集合 Iterator i=c.Iterator(); while(i.hasNext()) { Sy ...

  5. Iterator,foreach遍历小计

    此博客对同一操作对比两种遍历方式,以个人忘记时快速捡起为目的. 数据表: 三个List: List<Menu> menuList=menuService.getAllMenus(query ...

  6. Scala - Tips

    1- 运行scala命令,提示报错 问题现象: 在Windows7系统中安装scala后(直接安装MSI包,或者解压zip包添加环境变量的方式),执行scala命令报错,但可以执行scala -ver ...

  7. JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序

    前言:暑期应该开始了,因为小区对面的小学这两天早上都没有像以往那样一到七八点钟就人声喧闹.车水马龙. 前两篇文章介绍了Collection框架的主要接口和常用类,例如List.Set.Queue,和A ...

  8. Iterator && Iterable Collection && Map

    Java集合类库将集合的接口与实现分离.同样的接口,可以有不同的实现. Java集合类的基本接口是Collection接口.而Collection接口必须实现Iterable接口. 以下图表示集合框架 ...

  9. java 数据结构(八):Iterator接口与foreach循环

    1.遍历Collection的两种方式:① 使用迭代器Iterator ② foreach循环(或增强for循环)2.java.utils包下定义的迭代器接口:Iterator2.1说明:Iterat ...

  10. Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)

    首先,我们先看一下Collection集合的基本结构: 1.Collection接口 Collection是最基本集合接口,它定义了一组允许重复的对象.Collection接口派生了两个子接口Set和 ...

随机推荐

  1. windows下VisualStudio和QtCreator搭建Qt开发环境

    一.简介 集成开发平台IDE都有各自的长处,编写Qt程序可根据自己的喜好来选择相应的IDE.下述文章都是装载博友的文章,其中有很多细节还得自己调整. 二.详解 1.VisualStudio搭建Qt开发 ...

  2. mysql基础itcast笔记

    1. 课程回顾 mysql基础 1)mysql存储结构: 数据库 -> 表 -> 数据   sql语句 2)管理数据库: 增加: create database 数据库 default c ...

  3. 集成hibernateDaoSupport实现增删改查

    1. package edu.jlu.fuliang.dao.impl; import java.util.List; import org.springframework.orm.hibernate ...

  4. sublime取消自动升级提示

    1.进入Preferences -> Settings-User ,添加 "update_check": false, 2.重启Sublime.

  5. lua调用c函数

    参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...

  6. HDU - 5534 Partial Tree(每种都装的完全背包)

    Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...

  7. HTML5学习笔记(一)相关概率

    HTML5的设计目的是为了在移动设备上支持多媒体. 声明:<!DOCTYPE html> 注意:对于中文网页需要使用 <meta charset="utf-8"& ...

  8. Angular中依赖注入方式的几种写法

    1.第一种写法 angular.module('App').controller('TestCtrl',['$scope', function($scope) {}]); 2.第二种写法 angula ...

  9. 一些我推荐的和想上的网络课程(Coursera, edX, Udacity)

    从面向找工作的角度出发,我觉得以下课程有很大帮助: 首推Robert Sedgewick,也是我觉得对我帮助最大的老师,讲课特点是能把复杂的算法讲解清楚(典型例子:红黑树,KMP算法) 他在Cours ...

  10. 【Ionic+AngularJS 开发】之『个人日常管理』App(一)

      写在前面的话 过去一年自己接触了不少手机前端开发,得益于现在手机性能的提升和4G普及,感觉使用混合技术开发手机App已经可以满足越来越多的应用场景了.新年伊始,对自己2016年所学知识做一个阶段性 ...