Java中五個(gè)鮮為人知的Collections特性
簡(jiǎn)介
圖片
Java Collections框架提供了一套全面的接口和類,以高效地處理集合。Collections工具類提供了一些鮮為人知的功能。
在本文中,我們將通過(guò)簡(jiǎn)單的示例來(lái)探討這些鮮為人知的功能。
1. Collections.nCopies()
這個(gè)方法在Java中會(huì)返回一個(gè)不可變的列表,其中包含指定對(duì)象的n個(gè)副本。
Collections工具類中ncopies()的內(nèi)部代碼如下:
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()
這個(gè)方法在Java中用于查找給定集合中指定元素的頻率。
內(nèi)部代碼:
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()
這個(gè)方法在Java中提供了一種檢查兩個(gè)集合是否有任何共同元素的方法,如果有,則返回true,否則返回false。借助這個(gè)功能,開發(fā)者就可以快速查找集合中是否存在共同元素,而無(wú)需對(duì)它們進(jìn)行迭代。
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()
這個(gè)方法在Java中用于創(chuàng)建只包含單個(gè)元素的不可變集合。該方法返回一個(gè)僅包含單個(gè)元素的不可變Set。如果嘗試添加或刪除元素,就會(huì)出現(xiàn)異常。
內(nèi)部代碼:
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()
這個(gè)方法在Java中將指定列表的元素向指定距離進(jìn)行旋轉(zhuǎn)。
該方法對(duì)列表中的元素執(zhí)行回環(huán)變位(circular rotation),根據(jù)指定的距離有效地將它們向左或向右移動(dòng)。
內(nèi)部代碼:
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]
結(jié)論
在本文中,我們探討了Collections工具類的一些鮮為人知的功能。雖然Java Collections工具類提供了豐富的功能,但有一些鮮為人知的功能在某些情況下對(duì)開發(fā)者可能會(huì)很有用。