一、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}