1. 程式人生 > 實用技巧 >java集合Collection常用方法詳解

java集合Collection常用方法詳解

前言

出去面試的時候,對java的集合框架考察的知識點還是蠻多的。除了基礎的集合常見API使用,對集合底層的實現原理以及資料結構等也有很多考察方面。而自己對這方面知之甚少,特地抽空進行研究和學習一下。

為什麼要有集合

提到集合就不得不提一下陣列,好多集合底層都是依賴於陣列的實現。陣列一旦初始化後,長度就確定了,儲存資料物件不能達到動態擴充套件,其次陣列儲存元素不便於對陣列進行新增修改刪除操作,而且陣列可以儲存重複元素。這個時候集合對作用顯現出來了。集合分為CollectionMap兩種體系。下面先介紹Collection的集合類的繼承樹如下圖所示:

Collection介面是 (java.util.Collection)是Java集合類的頂級介面之一,整個集合框架就圍繞一組標準介面而設計,本文研究的集合基於JDK8的實現,下面將會進行多個集合類方法使用和主要方法原始碼分析,以作為後續出去面試的參考資料。

Collection方法介面介紹

Collection 介面有 3 種子型別集合:ListSetQueue,再下面是一些抽象類,最後是具體實現類,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、ArrayBlockingQueue等,下面是Collection的所有方法。

這些方法即可以操作Set集合,也可以操作Queue和List集合,下面分別使用Collection集合介面的方法說明

方法名說明
boolean add(E e) 向集合新增元素e,若指定集合元素改變了則返回true
boolean addAll(Collection<? extends E> c)
把集合C中的元素全部新增到集合中,若指定集合元素改變返回true
void clear() 清空所有集合元素
boolean contains(Object o) 判斷指定集合是否包含物件o
boolean containsAll(Collection<?> c) 判斷指定集合是否包含集合c的所有元素
boolean isEmpty() 判斷指定集合的元素size是否為0
boolean remove(Object o) 刪除集合中的元素物件o,若集合有多個o元素,則只會刪除第一個元素
boolean removeAll(Collection<?> c)
刪除指定集合包含集合c的元素
boolean retainAll(Collection<?> c) 從指定集合中保留包含集合c的元素,其他元素則刪除
int size() 集合的元素個數
T[] toArray(T[] a) 將集合轉換為T型別的陣列
    • 下面是主要方法的演示:
  @Test
    @SuppressWarnings("all")
    public void testCollection() {
        // 建立Collection介面的實現
        Collection collection = new ArrayList<>();
        // 新增元素
        collection.add("嘻嘻");
        String src = "????";
        collection.add(src);
        System.out.println(collection);

        // 建立Collection的實現
        Collection<String> coll = new HashSet<>();
        coll.add("?");
        coll.add("?");
        coll.add("?");
        System.out.println(coll);
        // 新增一個集合資料
        collection.addAll(coll);
        // 輸出集合的長度
        System.out.println(collection);
        // 判斷是否包含
        System.out.println(collection.contains("?"));
        // 移除元素
        collection.remove("?");
        // 新增物件
        collection.add(new Person("張三", 23, 5000d));
        // 當認為兩個物件屬性一致,相等時候,需重寫hashCode 和 equals方法
        System.out.println(collection.contains(new Person("張三", 23, 5000d)));

        System.out.println("-------");
        collection.add(null);
    
        Collection<String> collection1 = new ArrayList<>();
        collection1.add("嘻嘻");
        collection1.add("?");
        // 求兩個集合的交集(只保留collection1存在的元素)
        collection.retainAll(collection1);
        System.out.println(collection);
        // 清空元素
        collection.clear();
        System.out.println(collection);
    }

java8新特性操作集合

  • 使用lambda表示式遍歷集合

java8為Collection的父介面(Iterable)提供了一個預設的Foreach方法,我們可以使用它進行集合遍歷,若對lambda不太瞭解,不妨訪問一下這篇文章☞lambda表示式學習

   @Test
    public void testForeach() {
        Collection<String> collection = new ArrayList<>();
        collection.add("i");
        collection.add("love");
        collection.add("china");
        // foreach遍歷
        collection.forEach(e-> System.out.println(e));
        // 可以使用方法引用簡寫
        collection.forEach(System.out::println);
        // 或者迭代器的forEachRemaining方法
       collection.iterator().forEachRemaining(System.out::println);
    }

使用java8的predicate操作集合

    @Test
    public void testPredicate() {
        Collection<Integer> collection = new ArrayList<>();
        // 新增0-49
        for (int i = 0; i < 50; i++) {
            collection.add(i);
        }

        // 移除10-49的數字
        collection.removeIf(e -> (e > 9 && e < 50));
        System.out.println(collection);// 輸出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    }

基於流操作集合
java8之後引入了Stream相關流操作java集合,通過流大大簡化了對集合操作,關於這些流式操作,可以檢視這篇文章☞Stream入門學習,下面是基於流的一些簡單演示:

@Test
    public void testIntStream() {
        Collection<Integer> collection = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            collection.add(random.nextInt(100));
        }
        System.out.println(collection);
        // collection儲存的數值是包裝型別,可以將其轉換為IntStream
        IntStream intStream = collection.stream().mapToInt(e -> e);
        // intStream.forEach(System.out::println);
        System.out.println(collection.stream().mapToInt(e -> e).sum());
        // 輸出最大值
        collection.stream().mapToInt(e -> e).max().ifPresent(System.out::println);
        // 輸出最小值
        collection.stream().mapToInt(e -> e).min().ifPresent(System.out::println);
        // 統計大於50的數
        System.out.println(collection.stream().filter(e -> e > 50).count());
        // 原集合每一個值加1
        collection.stream().mapToInt(e-> e+1).forEach(System.out::println);
        // 排序
        collection.stream().mapToInt(e-> e).sorted().forEach(System.out::println);
        // 原數值每一個元素擴大2倍
        int[] ints = collection.stream().mapToInt(e -> e << 1).toArray();
        // 輸出原陣列
        System.out.println(Arrays.toString(ints));
        // 將陣列轉流
        IntStream stream = Arrays.stream(ints);
        // 輸出流平均數
        System.out.println(stream.average().getAsDouble());
    }