Collections class in java ?
The Collections
class in Java provides utility methods for working with collections, such as List
, Set
, and Map
. It offers various static methods to perform common operations like sorting, searching, and synchronizing collections. Here are some key functionalities provided by the Collections
class:
Sorting Collections:
sort(List<T> list)
: Sorts the specified list into ascending order.sort(List<T> list, Comparator<? super T> c)
: Sorts the specified list according to the order induced by the specified comparator.
Searching Collections:
binarySearch(List<? extends Comparable<? super T>> list, T key)
: Searches the specified list for the specified object using the binary search algorithm.binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
: Searches the specified list for the specified object using the binary search algorithm.
Synchronization of Collections:
synchronizedList(List<T> list)
: Returns a synchronized (thread-safe) list backed by the specified list.synchronizedSet(Set<T> s)
: Returns a synchronized (thread-safe) set backed by the specified set.synchronizedMap(Map<K,V> m)
: Returns a synchronized (thread-safe) map backed by the specified map.
Creating Immutable Collections:
emptyList()
,emptySet()
,emptyMap()
: Returns an immutable empty list, set, or map respectively.singletonList(T o)
,singleton(T o)
,singletonMap(K key, V value)
: Returns an immutable list, set, or map containing only the specified object or key-value pair.
Miscellaneous Utility Methods:
reverse(List<?> list)
: Reverses the order of the elements in the specified list.shuffle(List<?> list)
: Randomly permutes the elements in the specified list.copy(List<? super T> dest, List<? extends T> src)
: Copies all of the elements from one list into another.frequency(Collection<?> c, Object o)
: Returns the number of elements in the specified collection equal to the specified object.
Comments
Post a Comment