偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Java中五個鮮為人知的Collections特性

開發(fā) 前端
在本文中,我們探討了Collections工具類的一些鮮為人知的功能。雖然Java Collections工具類提供了豐富的功能,但有一些鮮為人知的功能在某些情況下對開發(fā)者可能會很有用。

簡介

圖片圖片

Java Collections框架提供了一套全面的接口和類,以高效地處理集合。Collections工具類提供了一些鮮為人知的功能。

在本文中,我們將通過簡單的示例來探討這些鮮為人知的功能。

1. Collections.nCopies()

這個方法在Java中會返回一個不可變的列表,其中包含指定對象的n個副本。

Collections工具類中ncopies()的內部代碼如下:

public static  List nCopies(int n, T o) {
        if (n < 0)
            throw new IllegalArgumentException("List length = " + n);
        return new CopiesList<>(n, o);
    }

示例:

public static void nCopies(){
     List tests = Collections.nCopies(10, "test");
     System.out.println(tests);
}
[test, test, test, test, test, test, test, test, test, test]

2. Collections.frequency()

這個方法在Java中用于查找給定集合中指定元素的頻率。

內部代碼:

public static int frequency(Collection c, Object o) {
        int result = 0;
        if (o == null) {
            for (Object e : c)
                if (e == null)
                    result++;
        } else {
            for (Object e : c)
                if (o.equals(e))
                    result++;
        }
        return result;
    }

示例:

public static void frequency(){
      List integers = List.of(1, 2, 3, 4, 5, 1, 2, 3, 2, 3, 4);
      int frequency = Collections.frequency(integers, 3);
      System.out.println(frequency);
 }    public static void singleton(){
        Set singleElement = Collections.singleton("Hello world");
        System.out.println(singleElement);
        singleElement.add("test");
    }    public static void singleton(){
        Set singleElement = Collections.singleton("Hello world");
        System.out.println(singleElement);
        singleElement.add("test");
    }

3. Collections.disjoint()

這個方法在Java中提供了一種檢查兩個集合是否有任何共同元素的方法,如果有,則返回true,否則返回false。借助這個功能,開發(fā)者就可以快速查找集合中是否存在共同元素,而無需對它們進行迭代。

public static boolean disjoint(Collection c1, Collection c2) { }

示例:

public static void disjoint(){
     List integers = List.of(1, 2, 3, 4);
     List integers1 = List.of(5, 6);
     boolean disjoint = Collections.disjoint(integers1, integers);
     System.out.println(disjoint);
        
     List integers2 = List.of(1, 2, 3, 4);
     boolean disjoint1 = Collections.disjoint(integers2, integers);
     System.out.println(disjoint1);
}
true
false

4.Collections.singleton()

這個方法在Java中用于創(chuàng)建只包含單個元素的不可變集合。該方法返回一個僅包含單個元素的不可變Set。如果嘗試添加或刪除元素,就會出現(xiàn)異常。

內部代碼:

public static  Set singleton(T o) {
    return new SingletonSet<>(o);
}

示例:

public static void singleton(){
        Set singleElement = Collections.singleton("Hello world");
        System.out.println(singleElement);
        singleElement.add("test");
    }
[Hello world]
Exception in thread "main" java.lang.UnsupportedOperationException
 at java.base/java.util.AbstractCollection.add(AbstractCollection.java:251)
 at misc.JavaCollectionFeatures.singleton(JavaCollectionFeatures.java:41)
 at misc.JavaCollectionFeatures.main(JavaCollectionFeatures.java:13)

5.Collections.rotate()

這個方法在Java中將指定列表的元素向指定距離進行旋轉。

該方法對列表中的元素執(zhí)行回環(huán)變位(circular rotation),根據(jù)指定的距離有效地將它們向左或向右移動。

內部代碼:

public static void rotate(List list, int distance) {
      if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
          rotate1(list, distance);
      else
          rotate2(list, distance);
}

示例:

public static void rotate(){
     List integers = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 5, 6);
     Collections.rotate(integers, 6);
     System.out.println(integers);
        
     List integers1 = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 5, 6);
     Collections.rotate(integers1, 10);
     System.out.println(integers1);
        
     List integers2 = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 5, 6);
     Collections.rotate(integers2, -3);
     System.out.println(integers2);
}
[4, 5, 3, 5, 5, 6, 1, 2, 3]
[6, 1, 2, 3, 4, 5, 3, 5, 5]
[4, 5, 3, 5, 5, 6, 1, 2, 3]

結論

在本文中,我們探討了Collections工具類的一些鮮為人知的功能。雖然Java Collections工具類提供了豐富的功能,但有一些鮮為人知的功能在某些情況下對開發(fā)者可能會很有用。


責任編輯:武曉燕 來源: Java學研大本營
相關推薦

2015-06-09 11:12:31

Swift語言Swift特性

2019-12-12 20:49:05

JavaScript語言運算符

2010-03-23 16:53:19

Visual Stud

2024-10-31 11:19:33

2023-05-29 19:39:40

Bash腳本

2011-07-29 11:13:42

2021-07-07 10:59:48

python代碼編程語言

2024-07-08 08:31:56

2009-09-14 09:45:20

Chrome谷歌操作系統(tǒng)

2023-04-23 15:11:26

2014-07-29 14:25:43

Unix命令

2019-10-08 16:24:33

Chrome瀏覽器

2022-11-30 16:31:48

CSS開發(fā)瀏覽器

2017-11-08 14:55:16

Linux命令sudo

2010-01-07 10:05:51

IT顧問特質

2020-06-05 14:11:21

Swift運算符代碼

2024-03-04 16:32:02

JavaScript運算符

2009-02-09 09:16:28

熱鍵自注銷漏洞

2011-05-03 13:13:52

編程PHPJava

2013-07-15 09:14:00

點贊
收藏

51CTO技術棧公眾號