📅  最后修改于: 2020-10-13 00:34:17             🧑  作者: Mango
Java集合类专用于对集合进行操作或返回集合的静态方法。它继承了Object类。
关于Java Collections类的要点是:
让我们来看一下java.util.Collections类的声明。
public class Collections extends Object
SN | Modifier & Type | Methods | Descriptions |
---|---|---|---|
1) | static |
addAll() | It is used to adds all of the specified elements to the specified collection. |
2) | static |
asLifoQueue() | It returns a view of a Deque as a Last-in-first-out (LIFO) Queue. |
3) | static |
binarySearch() | It searches the list for the specified object and returns their position in a sorted list. |
4) | static |
checkedCollection() | It is used to returns a dynamically typesafe view of the specified collection. |
5) | static |
checkedList() | It is used to returns a dynamically typesafe view of the specified list. |
6) | static |
checkedMap() | It is used to returns a dynamically typesafe view of the specified map. |
7) | static |
checkedNavigableMap() | It is used to returns a dynamically typesafe view of the specified navigable map. |
8) | static |
checkedNavigableSet() | It is used to returns a dynamically typesafe view of the specified navigable set. |
9) | static |
checkedQueue() | It is used to returns a dynamically typesafe view of the specified queue. |
10) | static |
checkedSet() | It is used to returns a dynamically typesafe view of the specified set. |
11) | static |
checkedSortedMap() | It is used to returns a dynamically typesafe view of the specified sorted map. |
12) | static |
checkedSortedSet() | It is used to returns a dynamically typesafe view of the specified sorted set. |
13) | static |
copy() | It is used to copy all the elements from one list into another list. |
14) | static boolean | disjoint() | It returns true if the two specified collections have no elements in common. |
15) | static |
emptyEnumeration() | It is used to get an enumeration that has no elements. |
16) | static |
emptyIterator() | It is used to get an Iterator that has no elements. |
17) | static |
emptyList() | It is used to get a List that has no elements. |
18) | static |
emptyListIterator() | It is used to get a List Iterator that has no elements. |
19) | static |
emptyMap() | It returns an empty map which is immutable. |
20) | static |
emptyNavigableMap() | It returns an empty navigable map which is immutable. |
21) | static |
emptyNavigableSet() | It is used to get an empty navigable set which is immutable in nature. |
22) | static |
emptySet() | It is used to get the set that has no elements. |
23) | static |
emptySortedMap() | It returns an empty sorted map which is immutable. |
24) | static |
emptySortedSet() | It is used to get the sorted set that has no elements. |
25) | static |
enumeration() | It is used to get the enumeration over the specified collection. |
26) | static |
fill() | It is used to replace all of the elements of the specified list with the specified elements. |
27) | static int | frequency() | It is used to get the number of elements in the specified collection equal to the specified object. |
28) | static int | indexOfSubList() | It is used to get the starting position of the first occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list. |
29) | static int | lastIndexOfSubList() | It is used to get the starting position of the last occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list. |
30) | static |
list() | It is used to get an array list containing the elements returned by the specified enumeration in the order in which they are returned by the enumeration. |
31) | static |
max() | It is used to get the maximum value of the given collection, according to the natural ordering of its elements. |
32) | static |
min() | It is used to get the minimum value of the given collection, according to the natural ordering of its elements. |
33) | static |
nCopies() | It is used to get an immutable list consisting of n copies of the specified object. |
34) | static |
newSetFromMap() | It is used to return a set backed by the specified map. |
35) | static |
replaceAll() | It is used to replace all occurrences of one specified value in a list with the other specified value. |
36) | static void | reverse() | It is used to reverse the order of the elements in the specified list. |
37) | static |
reverseOrder() | It is used to get the comparator that imposes the reverse of the natural ordering on a collection of objects which implement the Comparable interface. |
38) | static void | rotate() | It is used to rotate the elements in the specified list by a given distance. |
39) | static void | shuffle() | It is used to randomly reorders the specified list elements using a default randomness. |
40) | static |
singleton() | It is used to get an immutable set which contains only the specified object. |
41) | static |
singletonList() | It is used to get an immutable list which contains only the specified object. |
42) | static |
singletonMap() | It is used to get an immutable map, mapping only the specified key to the specified value. |
43) | static |
sort() | It is used to sort the elements presents in the specified list of collection in ascending order. |
44) | static void | swap() | It is used to swap the elements at the specified positions in the specified list. |
45) | static |
synchronizedCollection() | It is used to get a synchronized (thread-safe) collection backed by the specified collection. |
46) | static |
synchronizedList() | It is used to get a synchronized (thread-safe) collection backed by the specified list. |
47) | static |
synchronizedMap() | It is used to get a synchronized (thread-safe) map backed by the specified map. |
48) | static |
synchronizedNavigableMap() | It is used to get a synchronized (thread-safe) navigable map backed by the specified navigable map. |
49) | static |
synchronizedNavigableSet() | It is used to get a synchronized (thread-safe) navigable set backed by the specified navigable set. |
50) | static |
synchronizedSet() | It is used to get a synchronized (thread-safe) set backed by the specified set. |
51) | static |
synchronizedSortedMap() | It is used to get a synchronized (thread-safe) sorted map backed by the specified sorted map. |
52) | static |
synchronizedSortedSet() | It is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set. |
53) | static |
unmodifiableCollection() | It is used to get an unmodifiable view of the specified collection. |
54) | static |
unmodifiableList() | It is used to get an unmodifiable view of the specified list. |
55) | static |
unmodifiableMap() | It is used to get an unmodifiable view of the specified map. |
56) | static |
unmodifiableNavigableMap() | It is used to get an unmodifiable view of the specified navigable map. |
57) | static |
unmodifiableNavigableSet() | It is used to get an unmodifiable view of the specified navigable set. |
58) | static |
unmodifiableSet() | It is used to get an unmodifiable view of the specified set. |
59) | static |
unmodifiableSortedMap() | It is used to get an unmodifiable view of the specified sorted map. |
60 | static |
unmodifiableSortedSet() | It is used to get an unmodifiable view of the specified sorted set. |
import java.util.*;
public class CollectionsExample {
public static void main(String a[]){
List list = new ArrayList();
list.add("C");
list.add("Core Java");
list.add("Advance Java");
System.out.println("Initial collection value:"+list);
Collections.addAll(list, "Servlet","JSP");
System.out.println("After adding elements collection value:"+list);
String[] strArr = {"C#", ".Net"};
Collections.addAll(list, strArr);
System.out.println("After adding array collection value:"+list);
}
}
输出:
Initial collection value:[C, Core Java, Advance Java]
After adding elements collection value:[C, Core Java, Advance Java, Servlet, JSP]
After adding array collection value:[C, Core Java, Advance Java, Servlet, JSP, C#, .Net]
import java.util.*;
public class CollectionsExample {
public static void main(String a[]){
List list = new ArrayList();
list.add(46);
list.add(67);
list.add(24);
list.add(16);
list.add(8);
list.add(12);
System.out.println("Value of maximum element from the collection: "+Collections.max(list));
}
}
输出:
Value of maximum element from the collection: 67
import java.util.*;
public class CollectionsExample {
public static void main(String a[]){
List list = new ArrayList();
list.add(46);
list.add(67);
list.add(24);
list.add(16);
list.add(8);
list.add(12);
System.out.println("Value of minimum element from the collection: "+Collections.min(list));
}
}
输出:
Value of minimum element from the collection: 8