📜  Java Collections类

📅  最后修改于: 2020-10-13 00:34:17             🧑  作者: Mango

Java Collections类

Java集合类专用于对集合进行操作或返回集合的静态方法。它继承了Object类。

关于Java Collections类的要点是:

  • Java Collection类支持对集合进行操作的多态算法
  • 如果提供给它们的集合或类对象为null,则Java Collection类将引发NullPointerException

集合类声明

让我们来看一下java.util.Collections类的声明。

public class Collections extends Object
SN Modifier & Type Methods Descriptions
1) static boolean addAll() It is used to adds all of the specified elements to the specified collection.
2) static Queue asLifoQueue() It returns a view of a Deque as a Last-in-first-out (LIFO) Queue.
3) static int binarySearch() It searches the list for the specified object and returns their position in a sorted list.
4) static Collection checkedCollection() It is used to returns a dynamically typesafe view of the specified collection.
5) static List checkedList() It is used to returns a dynamically typesafe view of the specified list.
6) static Map checkedMap() It is used to returns a dynamically typesafe view of the specified map.
7) static NavigableMap checkedNavigableMap() It is used to returns a dynamically typesafe view of the specified navigable map.
8) static NavigableSet checkedNavigableSet() It is used to returns a dynamically typesafe view of the specified navigable set.
9) static Queue checkedQueue() It is used to returns a dynamically typesafe view of the specified queue.
10) static Set checkedSet() It is used to returns a dynamically typesafe view of the specified set.
11) static SortedMap checkedSortedMap() It is used to returns a dynamically typesafe view of the specified sorted map.
12) static SortedSet checkedSortedSet() It is used to returns a dynamically typesafe view of the specified sorted set.
13) static void 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 Enumeration emptyEnumeration() It is used to get an enumeration that has no elements.
16) static Iterator emptyIterator() It is used to get an Iterator that has no elements.
17) static List emptyList() It is used to get a List that has no elements.
18) static ListIterator emptyListIterator() It is used to get a List Iterator that has no elements.
19) static Map emptyMap() It returns an empty map which is immutable.
20) static NavigableMap emptyNavigableMap() It returns an empty navigable map which is immutable.
21) static NavigableSet emptyNavigableSet() It is used to get an empty navigable set which is immutable in nature.
22) static Set emptySet() It is used to get the set that has no elements.
23) static SortedMap emptySortedMap() It returns an empty sorted map which is immutable.
24) static SortedSet emptySortedSet() It is used to get the sorted set that has no elements.
25) static Enumeration enumeration() It is used to get the enumeration over the specified collection.
26) static void 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 ArrayList 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 > T max() It is used to get the maximum value of the given collection, according to the natural ordering of its elements.
32) static > T min() It is used to get the minimum value of the given collection, according to the natural ordering of its elements.
33) static List nCopies() It is used to get an immutable list consisting of n copies of the specified object.
34) static Set newSetFromMap() It is used to return a set backed by the specified map.
35) static boolean 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 Comparator 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 Set singleton() It is used to get an immutable set which contains only the specified object.
41) static List singletonList() It is used to get an immutable list which contains only the specified object.
42) static Map singletonMap() It is used to get an immutable map, mapping only the specified key to the specified value.
43) static >void 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 Collection synchronizedCollection() It is used to get a synchronized (thread-safe) collection backed by the specified collection.
46) static List synchronizedList() It is used to get a synchronized (thread-safe) collection backed by the specified list.
47) static Map synchronizedMap() It is used to get a synchronized (thread-safe) map backed by the specified map.
48) static NavigableMap synchronizedNavigableMap() It is used to get a synchronized (thread-safe) navigable map backed by the specified navigable map.
49) static NavigableSet synchronizedNavigableSet() It is used to get a synchronized (thread-safe) navigable set backed by the specified navigable set.
50) static Set synchronizedSet() It is used to get a synchronized (thread-safe) set backed by the specified set.
51) static SortedMap synchronizedSortedMap() It is used to get a synchronized (thread-safe) sorted map backed by the specified sorted map.
52) static SortedSet synchronizedSortedSet() It is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set.
53) static Collection unmodifiableCollection() It is used to get an unmodifiable view of the specified collection.
54) static List unmodifiableList() It is used to get an unmodifiable view of the specified list.
55) static Map unmodifiableMap() It is used to get an unmodifiable view of the specified map.
56) static NavigableMap unmodifiableNavigableMap() It is used to get an unmodifiable view of the specified navigable map.
57) static NavigableSet unmodifiableNavigableSet() It is used to get an unmodifiable view of the specified navigable set.
58) static Set unmodifiableSet() It is used to get an unmodifiable view of the specified set.
59) static SortedMap unmodifiableSortedMap() It is used to get an unmodifiable view of the specified sorted map.
60 static SortedSet unmodifiableSortedSet() It is used to get an unmodifiable view of the specified sorted set.

Java集合示例

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]

Java集合示例:max()

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

Java集合示例:min()

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