一、Collection接口java
(1)特色安全
(2)常见方法spa
二、Collection接口的经常使用方法code
(1)添加:对象
addblog
@Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println(collection); }
[zhai, true, 123]
addAll接口
public class DateDemo { @Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); Collection collection1=new ArrayList(); collection1.add("2020年8月4日20:35:10"); collection1.add("2020年8月4日20:35:30"); collection.addAll(collection1); System.out.println("批量添加后的collection:"+collection); } }
collection:[zhai, true, 123] 批量添加后的collection:[zhai, true, 123, 2020年8月4日20:35:10, 2020年8月4日20:35:30]
添加自定义类型的对象元素:rem
@Test public void test1(){ Collection collection=new ArrayList(); collection.add(new Student("zhai",12)); collection.add(new Student("zhao",13)); collection.add(new Student("zhang",14)); collection.add(new Student("li",12)); System.out.println(collection); }
[Student{sname='zhai', age=12}, Student{sname='zhao', age=13}, Student{sname='zhang', age=14}, Student{sname='li', age=12}]
(2)删除get
removeit
@Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); collection.remove(123); System.out.println("删除后:"+collection); }
removeAll
@Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); Collection collection1=new ArrayList(); collection1.add("2020年8月4日20:35:10"); collection1.add("2020年8月4日20:35:30"); collection.addAll(collection1); System.out.println("批量添加后的collection:"+collection); collection.removeAll(collection1); System.out.println("批量删除后:"+collection); }
collection:[zhai, true, 123] 批量添加后的collection:[zhai, true, 123, 2020年8月4日20:35:10, 2020年8月4日20:35:30] 批量删除后:[zhai, true, 123]
(3)查找
contains
@Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); System.out.println(collection.contains("zhai")); }
collection:[zhai, true, 123] true
containsAll
@Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); Collection collection1=new ArrayList(); collection1.add("2020年8月4日20:35:10"); collection1.add("2020年8月4日20:35:30"); collection.addAll(collection1); System.out.println("批量添加后的collection:"+collection); System.out.println(collection.containsAll(collection1)); }
collection:[zhai, true, 123] 批量添加后的collection:[zhai, true, 123, 2020年8月4日20:35:10, 2020年8月4日20:35:30] true
(4)获取元素个数
@Test public void test1(){ Collection collection=new ArrayList();//建立Collection接口对象 collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); System.out.println(collection.size()); }
collection:[zhai, true, 123] 3
(5)清除
@Test public void test1(){ Collection collection=new ArrayList(); collection.add("zhai"); collection.add(true); collection.add(123); System.out.println("collection:"+collection); collection.clear(); System.out.println(collection); System.out.println(collection.size()); }
collection:[zhai, true, 123] [] 0
(6)判断是否为空
@Test public void test1(){ Collection collection=new ArrayList(); collection.add("zhai"); collection.add(true); collection.add(123); System.out.println(collection.isEmpty()); System.out.println("collection:"+collection); collection.clear(); System.out.println(collection); System.out.println(collection.size()); System.out.println(collection.isEmpty()); }
false collection:[zhai, true, 123] [] 0 true
集合为空的时候返回true,不然返回false
三、遍历
(1)迭代器
流程:
特色:
每次只能下移一位
只能下移
遍历方式:
public class DateDemo { @Test public void test1(){ Collection collection=new ArrayList(); collection.add(new Student("zhai",12)); collection.add(new Student("zhao",13)); collection.add(new Student("zhang",14)); collection.add(new Student("li",12)); Iterator iterator=collection.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
Student{sname='zhai', age=12} Student{sname='zhao', age=13} Student{sname='zhang', age=14} Student{sname='li', age=12}
@Test public void test1(){ Collection collection=new ArrayList(); collection.add(new Student("zhai",12)); collection.add(new Student("zhao",13)); collection.add(new Student("zhang",14)); collection.add(new Student("li",12)); Iterator iterator=collection.iterator(); while (iterator.hasNext()){ collection.add(new Student("li",10)); System.out.println(iterator.next()); } }
java.util.ConcurrentModificationException
默认的元素个数是4,每次遍历的时候都会计算元素的个数,只要影响了实际的元素个数就会发生异常(底层存在一个变量存储默认值为0,在建立了迭代器对象以后该变量的值变为集合的大小,每次调用next都会验证当前集合的大小和变量值的大小,不相等会抛出异常),所以,进行添加、删除操做就会抛出异常。可以维护数据的安全性。
修改没有影响元素的个数,不会抛出此异常
通常迭代的时候是不会进行增长操做,删除的时候能够使用迭代器自己的remove方法
@Test public void test1(){ Collection collection=new ArrayList(); collection.add(new Student("zhai",12)); collection.add(new Student("zhao",13)); collection.add(new Student("zhang",14)); collection.add(new Student("li",12)); Iterator iterator=collection.iterator(); while (iterator.hasNext()){ Student next= (Student) iterator.next(); if (next.getAge()<13){ iterator.remove(); } } for (Object s:collection){ System.out.println(s); } }
(2)加强for
@Test public void test1(){ Collection collection=new ArrayList(); collection.add(new Student("zhai",12)); collection.add(new Student("zhao",13)); collection.add(new Student("zhang",14)); collection.add(new Student("li",12)); for(Object student:collection){ System.out.println(student); } }
Student{sname='zhai', age=12} Student{sname='zhao', age=13} Student{sname='zhang', age=14} Student{sname='li', age=12}