溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java集合Collection常用方法有哪些

發(fā)布時(shí)間:2023-03-13 13:51:12 來源:億速云 閱讀:100 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了java集合Collection常用方法有哪些的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇java集合Collection常用方法有哪些文章都會(huì)有所收獲,下面我們一起來看看吧。

為什么要有集合

提到集合就不得不提一下數(shù)組,好多集合底層都是依賴于數(shù)組的實(shí)現(xiàn)。數(shù)組一旦初始化后,長(zhǎng)度就確定了,存儲(chǔ)數(shù)據(jù)對(duì)象不能達(dá)到動(dòng)態(tài)擴(kuò)展,其次數(shù)組存儲(chǔ)元素不便于對(duì)數(shù)組進(jìn)行添加、修改、刪除操作,而且數(shù)組可以存儲(chǔ)重復(fù)元素。

這個(gè)時(shí)候集合對(duì)作用顯現(xiàn)出來了。

集合分為CollectionMap兩種體系。

下面先介紹Collection的集合類的繼承樹如下圖所示:

java集合Collection常用方法有哪些

Collection接口是 (java.util.Collection)是Java集合類的頂級(jí)接口之一,整個(gè)集合框架就圍繞一組標(biāo)準(zhǔn)接口而設(shè)計(jì)。

Collection方法接口介紹

Collection 接口有 3 種子類型集合: List、SetQueue,再下面是一些抽象類,最后是具體實(shí)現(xiàn)類,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、ArrayBlockingQueue等,下面是Collection的所有方法。

java集合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)判斷指定集合是否包含對(duì)象o
boolean containsAll(Collection<?> c)判斷指定集合是否包含集合c的所有元素
boolean isEmpty()判斷指定集合的元素size是否為0
boolean remove(Object o)刪除集合中的元素對(duì)象o,若集合有多個(gè)o元素,則只會(huì)刪除第一個(gè)元素
boolean removeAll(Collection<?> c)刪除指定集合包含集合c的元素
boolean retainAll(Collection<?> c)從指定集合中保留包含集合c的元素,其他元素則刪除
int size()集合的元素個(gè)數(shù)
T[] toArray(T[] a)將集合轉(zhuǎn)換為T類型的數(shù)組

下面是主要方法的演示:

  @Test
    @SuppressWarnings("all")
    public void testCollection() {
        // 創(chuàng)建Collection接口的實(shí)現(xiàn)
        Collection collection = new ArrayList<>();
        // 添加元素
        collection.add("嘻嘻");
        String src = "????";
        collection.add(src);
        System.out.println(collection);

        // 創(chuàng)建Collection的實(shí)現(xiàn)
        Collection<String> coll = new HashSet<>();
        coll.add("?");
        coll.add("?");
        coll.add("?");
        System.out.println(coll);
        // 添加一個(gè)集合數(shù)據(jù)
        collection.addAll(coll);
        // 輸出集合的長(zhǎng)度
        System.out.println(collection);
        // 判斷是否包含
        System.out.println(collection.contains("?"));
        // 移除元素
        collection.remove("?");
        // 添加對(duì)象
        collection.add(new Person("張三", 23, 5000d));
        // 當(dāng)認(rèn)為兩個(gè)對(duì)象屬性一致,相等時(shí)候,需重寫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("?");
        // 求兩個(gè)集合的交集(只保留collection1存在的元素)
        collection.retainAll(collection1);
        System.out.println(collection);
        // 清空元素
        collection.clear();
        System.out.println(collection);
    }

java8新特性操作集合

使用lambda表達(dá)式遍歷集合

java8為Collection的父接口(Iterable)提供了一個(gè)默認(rèn)的Foreach方法,我們可以使用它進(jìn)行集合遍歷,若對(duì)lambda不太了解,不妨訪問一下這篇文章? lambda表達(dá)式學(xué)習(xí)

   @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));
        // 可以使用方法引用簡(jiǎn)寫
        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的數(shù)字
        collection.removeIf(e -> (e > 9 && e < 50));
        System.out.println(collection);// 輸出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    }

基于流操作集合

java8之后引入了Stream相關(guān)流操作java集合,通過流大大簡(jiǎn)化了對(duì)集合操作,關(guān)于這些流式操作,可以查看這篇文章? Stream入門學(xué)習(xí),下面是基于流的一些簡(jiǎn)單演示:

@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存儲(chǔ)的數(shù)值是包裝類型,可以將其轉(zhuǎn)換為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);
        // 統(tǒng)計(jì)大于50的數(shù)
        System.out.println(collection.stream().filter(e -> e > 50).count());
        // 原集合每一個(gè)值加1
        collection.stream().mapToInt(e-> e+1).forEach(System.out::println);
        // 排序
        collection.stream().mapToInt(e-> e).sorted().forEach(System.out::println);
        // 原數(shù)值每一個(gè)元素?cái)U(kuò)大2倍
        int[] ints = collection.stream().mapToInt(e -> e << 1).toArray();
        // 輸出原數(shù)組
        System.out.println(Arrays.toString(ints));
        // 將數(shù)組轉(zhuǎn)流
        IntStream stream = Arrays.stream(ints);
        // 輸出流平均數(shù)
        System.out.println(stream.average().getAsDouble());
    }

關(guān)于“java集合Collection常用方法有哪些”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“java集合Collection常用方法有哪些”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI