1. 程式人生 > 實用技巧 >集合:Collection介面(常用方法、遍歷)

集合:Collection介面(常用方法、遍歷)

1、Collection介面

(1)特點

  • 沒有直接的實現類,而是提供了子介面
  • 該介面提供了一系列常見的集合操作的方法,例如:增加、刪除、查詢

(2)常見方法

  • add:單個新增
  • addAll:批量新增
  • remove:刪除
  • removeAll:批量刪除
  • contains:查詢
  • containsAll:批量查詢

2、Collection介面的常用方法

(1)新增:

add

@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]

新增自定義型別的物件元素:

@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)刪除

remove

 @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

3、遍歷

(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}
  • 本質上是迭代器,只是語法簡化了