Java- java-util-Collections

java.util.Collections class

This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection, and a few other odds and ends.

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

  • Create List/Set/Map from Arrays

  • Converting List/Set/Map to Synchronized & unmodifiable

  • Sorting & Searching

  • Reverse, shuffle, min,max,copy ```java List list = List.of("foo", "bar", "baz");

Set set = Set.of("foo", "bar", "baz");

Map<String, String> map = Map.of(“foo”, “a”, “bar”, “b”, “baz”, “c”);

Map<String, String> map = Map.ofEntries( new AbstractMap.SimpleEntry<>(“foo”, “a”), new AbstractMap.SimpleEntry<>(“bar”, “b”), new AbstractMap.SimpleEntry<>(“baz”, “c”)); ```


1.static void sort(List list1): Sorts the list list1 into ascending order according to the natural sequence (a before b or 1 before 2) of the elements.

2.static int binarySearch(List list1, Object obj1): Searches the obj1 in the list list1. Returns the index number of the element obj1. Before applying this method, the elements must be sorted earlier with sort() method

3.static Collection synchronizedCollection(Collection col1): Returns a synchronized version of collection col1. It is used for thread-safe operations.We have following specific Methods like

  • synchronizedList()

  • synchronizedSet()

  • synchronizedSortedSet()

  • synchronizedMap()

  • synchronizedSortedMap()

4. static Collection unmodifiableCollection(Collection col1): make any Collection to unmodifiable (read-only) version of col1ection. Any methods like add() or remove(), if applied throws UnsupportedOperationException We have following specific Methods like

  • unmodifiableList()

  • unmodifiableSet()

  • unmodifiableSortedSet()

  • unmodifiableMap(),

  • unmodifiableSortedMap()., etc

5.static void reverse(List list1): Existing order of the elements in the list list1 are reversed.

6.static void shuffle(List list1): Shuffles the existing elements of list1 randomly. For repeated execution of the method, elements with different order are obtained.

7.static void swap(List list1, int index1, int index2): List list1 elements at index numbers index1 and index2 are swapped.

8.static void fill(List list1, Object obj1): Replaces all the elements of list1 with obj1. Earlier elements are lost. This method is used to fill all the elements with the same values.

9.static void copy(List destination1, List source1): Copies all the elements of List source1into the destination1 list. It is like arraycopy() method.

10.static Object min(Collection col1): Returns the element with the minimum value

11.static Object max(Collection col1): Returns the element with the maximum value

12.static void rotate(List list1, int dist1): Rotates the elements in the list list1 by the specified distance dist1.

13.static boolean replaceAll(List list1, Object oldObj, Object newObj): Replaces the old element oldObj with the new element newObj in the list list1 (all the occurrences). Returns true when the operation is successful (when the **oldObj **exists).

14.static int frequency(Collection col1, Object obj1): Checks how many times obj1 exists in collection col1, returns as an integer value.

15.static boolean disjoint(Collection col1, Collection col2): Returns true if the collection classes col1 and col2 do not have any common elements. Introduced with JDK 1.5.

16.static boolean addAll(Collection col1, Object obj1): Here, obj1 can be a single element or an array. Adds obj1 to the collection col1.