Introduction to common methods of Collections class

The Java Collections class is one of the most important parts of the Java Collections framework. It provides a common set of tools to help developers use collections of various types in their applications. The Collections class provides methods designed to simplify the process of sorting, searching, and manipulating collections.

This article will introduce some commonly used Java Collections class methods, and the names, parameters and return values ​​of these methods are shown in the table below.

basic operation

method name method parameters method return value method description
sort List list void Sort the list in ascending order
reverse List list void reverse the specified list
shuffle List list void random permutation list
fill List<? super T> list, T obj void replaces all elements in the specified list with a specific element
copy List<? super T> dest, List<? extends T> src void copies all elements of the source list to the destination list

These methods allow developers to easily manipulate lists without writing the code themselves. For example, the sort() method can sort a list in ascending order, while the reverse() method can reverse the specified list, the shuffle() method can randomly replace the list elements, and the fill() method can replace all elements in the specified list with a specific element, The copy() method can copy all elements of the source list to the target list.

binary search

method name method parameters method return value method description
binarySearch List<? extends Comparable<? super T>> list, T key int perform a binary search in an ordered list
binarySearch List<? extends T> list, T key, Comparator<? super T> c int Perform a binary search in an ordered list, using a custom comparator for comparison

These methods allow developers to perform binary searches on ordered lists. The first method, binarySearch(), takes an ordered list and the element to search for, and it will return the position where the element was found, or a negative number if it was not found. The second method, binarySearch(), allows developers to use a custom comparator to determine how elements in an ordered list are sorted.

synchronous control

method name method parameters method return value method description
synchronizedCollection Collection c Collection Wraps the specified collection as a thread-safe collection
synchronizedList List list List wraps the specified list as a thread-safe list
synchronizedMap Map<K,V> m Map<K,V> Wraps the specified map as a thread-safe map
synchronizedSet Set s Set Wraps the specified collection as a thread-safe collection
synchronizedSortedMap SortedMap<K,V> m SortedMap<K,V> Wraps the specified sorted map as a thread-safe map
synchronizedSortedSet SortedSet s SortedSet Wraps the specified sorted collection as a thread-safe sorted collection

These methods convert collections to thread-safe versions so that they can be safely accessed from multiple threads. For example, the synchronizedList() method wraps the original list into a thread-safe list, preventing multiple threads from accessing and modifying the list at the same time.

immutable object

method name method parameters method return value method description
emptyList none List return an empty list
emptyMap none Map<K,V> returns an empty map
emptySet none Set returns an empty immutable collection
singletonList T o List Returns a list containing only the specified objects
singletonMap K key, V value Map<K,V> Returns a mapping table containing only the specified mappings

These methods can create some immutable collection objects. Immutable objects are objects that, once created, cannot be modified. For example, the emptyList() method can create an empty list, and the singletonList() method can create a list containing only the specified objects.

type conversion

method name method parameters method return value method description
toArray T[] a Object[] convert list to array
asList T… a List Convert an array to a list
frequency Collection<?> c, Object o int Returns the number of occurrences of the specified element in the specified collection
disjoint Collection<?> c1, Collection<?> c2 boolean Check if two sets intersect
addAll Collection<? super T> c, T… elements boolean Add a batch of elements to a collection

These methods convert a collection of one type to another. For example, the asList() method converts an array into a list, and the frequency() method returns the number of occurrences of a specified element in a specified collection.

other

method name method parameters method return value method description
max Collection<? extends T> coll, Comparator<? super T> comp T Get the largest element according to the specified comparator
min Collection<? extends T> coll, Comparator<? super T> comp T Get the minimum element according to the specified comparator
rotate List<?> list, int distance void rotate the elements in the specified list
replaceAll List list, T oldVal, T newVal boolean Replace all old elements in the list with new elements
indexOfSubList List<?> source, List<?> target int Find the position of the first occurrence of a sublist

These methods provide some additional functions, such as max() and min() methods to obtain the largest or smallest element in the collection according to the specified Comparator. The rotate() method can rotate the elements in the specified list, the replaceAll() method can replace all the old elements in the list with new elements, and the indexOfSubList() method can find the position of the sublist in the parent list.

The above are some methods of the commonly used Java Collections class, including method parameters and method return values. These methods can help developers complete various collection operations efficiently and improve the readability and maintainability of the code.

Guess you like

Origin blog.csdn.net/qq_25000135/article/details/129993761