📜  Java.util.Collections类

📅  最后修改于: 2020-11-14 06:14:14             🧑  作者: Mango


介绍

java.util.Collections类仅由对集合进行操作或返回集合的静态方法组成。以下是有关Collections的重点-

  • 它包含对集合“包装器”进行操作的多态算法,该算法返回由指定集合支持的新集合。

  • 如果提供给它们的集合或类对象为null,则此类的所有方法都将引发NullPointerException。

类声明

以下是java.util.Collections类的声明-

public class Collections
   extends Object

领域

以下是java.util.Collections类的字段-

  • 静态列表EMPTY_LIST-这是空列表(不可变)。

  • 静态映射EMPTY_MAP-这是空映射(不可变)。

  • 静态集EMPTY_SET-这是空集(不可变)。

类方法

Sr.No. Method & Description
1 static boolean addAll(Collection c, T… elements)

This method adds all of the specified elements to the specified collection.

2 static Queue asLifoQueue(Deque deque)

This method returns a view of a Deque as a Last-in-first-out (Lifo) Queue.

3 static int binarySearch(List> list, T key)

This method searches the specified list for the specified object using the binary search algorithm.

4 static int binarySearch(List list, T key, Comparator

This method searches the specified list for the specified object using the binary search algorithm.

5 static Collection checkedCollection(Collection c, Class type)

This method returns a dynamically typesafe view of the specified collection.

6 static ListcheckedList(List list, Class type)

This method returns a dynamically typesafe view of the specified list.

7 static Map checkedMap(Map m, Class keyType, Class valueType)

This method returns a dynamically typesafe view of the specified map.

8 static Set checkedSet(Set s, Class type)

This method returns a dynamically typesafe view of the specified set.

9 static SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType)

This method returns a dynamically typesafe view of the specified sorted map.

10 static SortedSetcheckedSortedSet(SortedSet s, Class type)

This method returns a dynamically typesafe view of the specified sorted set.

11 static voidcopy(List dest, List src)

This method copies all of the elements from one list into another.

12 static boolean disjoint(Collection c1, Collection c2)

This method returns true if the two specified collections have no elements in common.

13 static List emptyList()

This method returns the empty list (immutable).

14 static MapemptyMap()

This method returns the empty map (immutable).

15 static SetemptySet()

This method returns the empty set (immutable).

16 static Enumeration enumeration(Collection c)

This method returns an enumeration over the specified collection.

17 static void fill(List list, T obj)

This method replaces all of the elements of the specified list with the specified element.

18 static int frequency(Collection c, Object o)

This method returns the number of elements in the specified collection equal to the specified object.

19 static int indexOfSubList(List source, List target)

This method returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

20 static int lastIndexOfSubList(List source, List target)

This method returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

21 static ArrayList list(Enumeration e)

This method returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

22 static >T max(Collection coll)

This method returns the maximum element of the given collection, according to the natural ordering of its elements.

23 static T max(Collection coll, Comparator comp)

This method returns the maximum element of the given collection, according to the order induced by the specified comparator.

24 static >T min(Collection coll)

This method Returns the minimum element of the given collection, according to the natural ordering of its elements.

25 static T min(Collection coll, Comparator comp)

This method returns the minimum element of the given collection, according to the order induced by the specified comparator.

26 static List nCopies(int n, T o)

This method returns an immutable list consisting of n copies of the specified object.

27 static Set newSetFromMap(Map map)

This method returns a set backed by the specified map.

28 static boolean replaceAll(List list, T oldVal, T newVal)

This method replaces all occurrences of one specified value in a list with another.

29 static void reverse(List list)

This method reverses the order of the elements in the specified list.

30 static Comparator reverseOrder()

This method returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

31 static Comparator reverseOrder(Comparator cmp)

This method returns a comparator that imposes the reverse ordering of the specified comparator.

32 static void rotate(List list, int distance)

This method rotates the elements in the specified list by the specified distance.

33 static void shuffle(List list)

This method randomly permutes the specified list using a default source of randomness.

34 static void shuffle(List list, Random rnd)

This method randomly permute the specified list using the specified source of randomness.

35 static Set singleton(T o)

This method returns an immutable set containing only the specified object.

36 static List singletonList(T o)

This method returns an immutable list containing only the specified object.

37 static MapsingletonMap(K key, V value)

This method returns an immutable map, mapping only the specified key to the specified value.

38 static > voidsort(List list)

This method sorts the specified list into ascending order, according to the natural ordering of its elements.

39 static void sort(List list, Comparator c)

This method sorts the specified list according to the order induced by the specified comparator.

40 static void swap(List list, int i, int j)

This method swaps the elements at the specified positions in the specified list.

41 static Collection synchronizedCollection(Collection c)

This method returns a synchronized (thread-safe) collection backed by the specified collection.

42 static List synchronizedList(List list)

This method returns a synchronized (thread-safe) list backed by the specified list.

43 static MapsynchronizedMap(Map m)

This method returns a synchronized (thread-safe) map backed by the specified map.

44 static SetsynchronizedSet(Set s)

This method returns a synchronized (thread-safe) set backed by the specified set.

45 static SortedMap synchronizedSortedMap(SortedMap m)

This method returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

46 static SortedSet synchronizedSortedSet(SortedSet s)

This method returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

47 static Collection unmodifiableCollection(Collection c)

This method returns an unmodifiable view of the specified collection.

48 static List unmodifiableList(List list)

This method returns an unmodifiable view of the specified list.

49 static MapunmodifiableMap(Map m)

This method returns an unmodifiable view of the specified map.

50 static Set unmodifiableSet(Set s)

This method returns an unmodifiable view of the specified set.

51 static SortedMap unmodifiableSortedMap(SortedMap m)

This method returns an unmodifiable view of the specified sorted map.

52 static SortedSetunmodifiableSortedSet(SortedSet s)

This method returns an unmodifiable view of the specified sorted set.

方法继承

此类从以下类继承方法-

  • java.util.Object