Collections Class in Java

The Collection class in Java is used with various static methods to perform the desired task. In this article, you will get to know brief details about those methods in the Java Collections class.

What is a Java Collections class?

The java collection class contains static methods that work on or return collections. Here are a couple of important points about Collections:

  • It supports polymorphic algorithms that operate on collections.
  • It throws a NullPointerException when the given collections or class objects are null.

Collection Class declaration in Java:

The declaration of the java.util.Collections class:

public class Collections extends Object

Fields in Java Collections class:

The fields in the Java Collection class are as follows:

  • static List EMPTY_LIST – It is an immutable empty list.
  • static Map EMPTY_MAP – This is an immutable empty map.
  • static Set EMPTY_SET – This is an immutable empty set.
S.no Modifier & Type Methods Description
1 static <T> boolean addAll() It adds all the mentioned elements to the specified collection.
2 static <T> Queue<T> asLifoQueue() It returns a Deque view as a Last-In-first-Out Queue.
3 static <T> int binarySearch() It searches for a particular object from the list and returns its position in a sorted list.
4 static <E> Collection<E> checkedCollection() It returns a dynamically typesafe view of the particular collection.
5 static <E> List<E> checkedList() It returns a typesafe view of the particular list dynamically. 
6 static <K,V> Map<K,V> checkedMap() It returns a typesafe view of the specified map.
7 static <K,V> NavigableMap<K,V> checkedNavigableMap() It returns a dynamically typesafe view of the specified navigable set.
8 static <E> NavigableSet<E> checkedNavigableSet() It returns a dynamically typesafe view of the specified set.
9 static <E> Queue<E>

 

checkedQueue() It returns a dynamically, typesafe view of the specified type.
10 static <E> Set<E> checkedSet() It returns a dynamically typesafe view of the specified sorted map.
11 static <K,V> SortedMap<K,V> checkedSortedMap() it returns a dynamically typesafe view of the specified sorted set.
12 static <E> SortedSet<E> checkedSortedSet() it copies all the elements from one list into another list.
13 static <T> void copy() It copies all the elements from one list to another.
14 static boolean disjoint() It returns true if the mentioned collections have no common elements.
15 static <T> Enumeration<T> emptyEnumeration() It gets an enumeration with no elements.
16 static <T> Iterator<T> emptyIterator() It gets an iterator with no elements.
17 static <T> List<T> emptyList() It gets a list with no elements.
18 static <T> ListIterator<T> emptyListIterator() It gets a List Iterator with no elements.
19 static <K, V> Map<K,V> emptyMap() It returns an empty immutable map.
20 static <K,V> NavigableMap<K,V> emptyNavigableSet() It returns an empty navigable immutable map.
21 static <E> NavigableSet<E> emptyNavigableSet() It gets an empty navigable set that is immutable in nature.
22 static <T> Set<T> emptySet() It gets the set with no elements.
23 static <K,V> SortedMap<K,V> emptySortedMap() It returns an empty sorted immutable map
24 static <E> SortedSet<E> emptySortedSet() It gets the sorted set with no elements.
25 static <T> Enumeration<T> enumeration() It gets the numeration over the mentioned collection.
26 static<T> void fill() It replaces all the elements in the mentioned list with the given elements.
27 static int frequency() It gets the number of elements in the specified collection equal to the specified object.
28 static int indexOfSubList() It gets the starting position of the first occurrence of the specified target list within the mentioned source list. It returns -1 if there is no such occurrence in the list.
29 static int lastIndexOfSubList() It gets the beginning position of the last occurrence of the specified target list within the mentioned source list. It returns -1 if there is no such occurrence in the list.
30 static <T> ArrayList<T> list() It gets an array list with elements returned by the specified enumeration in the order in which they are returned by the enumeration.
31 static <T extends Object & Comparable<? super>> T max() It gets the maximum value of the given collection according to the natural ordering of the elements.
32 static <T extends Object & Comparable<? Super T>> T min() It gets the minimum value of the given collection according to the natural ordering of the elements.
33 static <T> List<T> nCopies() It gets an immutable list with n copies of the mentioned object.
34 static <E> Set<E> newSetFromMap() It returns a set backed up by the specified map.
35 static <T> boolean replaceAll() It replaces the occurrences of the specified value in the list with another value.
36 static void reverse() It uses to reverse the order of elements in the list.
37 static <T> Comparator<T> reverseOrder() It gets the comparator that imposes the reverse of natural ordering on a collection of objects that implement the comparable interface.
38 static void rotate() It uses to rotate the elements in the specified list with a given distance.
39 static void shuffle() It randomly reorders the mentioned list elements with default randomness.
40 static <T> Set<T> singleton() It gets an immutable set with only the specified object.
41 static <T> List<T> singletonList() It gets an immutable list with only the specified object.
42 static <K,V> Map<K,V> singletonMap() It gets an immutable map, mapping only the specified key to the specified value.
43 static <T extends Comparable<? Super T>> void sort() It sorts the elements present in the specified list of collections in ascending order.
44 static void swap() It swaps the elements at the specified positions in the mentioned list.
45 static <T> Collection<T> synchronizedCollection() It gets a synchronized (thread-safe) collection backed by the mentioned collection.
46 static <T> List<T> synchronizedList() It gets a synchronized (thread-safe) collection backed by the mentioned list.
47 static <K,V> Map<K,V> synchronizedMap() It gets a synchronized (thread-safe) map backed by the mentioned map.
48 static <K,V> NavigableMap<K,V> synchronizedNavigableMap() It gets a synchronized (thread-safe) navigable map backed by the mentioned navigable map.
49 static <T> NavigaleSet<T> synchronizedNavigableSet() It gets a synchronized (thread-safe) navigable set backed by the mentioned navigable set.
50 static <T> Set<T> synchronizedSet() It gets a synchronized (thread-safe) set backed by the mentioned set.
51 static <K,V> SortedMap<K,V> synchronizedSortedMap() It gets a synchronized (thread-safe) sorted map backed by the mentioned sorted map.
52 static <T> SortedSet<T> synchronizedSortedSet() It gets a synchronized (thread-safe) sorted set backed by the mentioned sorted set.
53 static <T> Collection<T> unmodifiableCollection() It gets an unmodifiable view of the mentioned collection.
54 static <T> List<T> unmodifiableList() It gets an unmodifiable view of the mentioned list.
55 static <K,V> Map<K,V> unmodifiableMap() It gets an unmodifiable view of the specified map.
56 static <K,V> NavigableMap<T> unmodifiableNavigableMap() It gets an unmodifiable view of the mentioned navigable map.
57 static <K,V> NavigableSet<T> unmodifiableNavigableSet() It gets an unmodifiable view of the mentioned navigable set.
58 static <T> Set<T> unmodifiableSet() It gets an unmodifiable view of the mentioned set.
59 static <K,V> SortedMap<K,V> unmodifiableSortedMap() It gets an unmodifiable view of the mentioned sorted map.
60 static <T> SortedSet<T> unmodifiableSortedSet() It gets an unmodifiable view of the specified sorted set.

Other methods:

Methods Description
max(Collection<?extends T> coll) It returns the maximum element of the collection, according to the natural ordering of its elements.
min(Collection<?extends T> coll) It returns the minimum element of the collection, according to the natural ordering of its elements.
shuffle(List<?> list Random rnd) It randomly permutes the particular list using the specified source of randomness.

Java Collections Example:

import java.util.*;
public class FirstCode{
public static void main(String args[]){
List<String> l = new ArrayList<String>();
l.add(“C”);
l.add(“C++”);
l.add(“Java”);
System.out.println(“The Initial collection value is: ”+l);
Collections.addAll(l, “Python”, “JavaScript”);
System.out.println(“After adding elements to the Collection: ”+l);
String[] s = {“.Net”, “C#”};
Collections.addAll(l, s); 
System.out.println(“After adding array collection value: ”+l);
}
}

Output:

The Initial collection value is: [C, C++, Java]

After adding elements to the Collection: [Python, JavaScript]

After adding array collection value: [.Net, C#]

Sample program for Java Collections using the max() method:

import java.util.*;
public class FirstCode{
public static void main(String a[]){
List<Integer> l = new ArrayList<Integer>();
l.add(5);
l.add(10);
l.add(12);
l.add(16);
l.add(23);
l.add(6);
System.out.println(“The maximum element from the collection: ”+Collections.max(list));
}
}

Output:

The maximum element from the collection: 23

Sample program for Java Collections using the min() method:

import java.util.*;
public class FirstCode{
public static void main(String a[]){
List<Integer> l = new ArrayList<Integer>();
l.add(5);
l.add(10);
l.add(12);
l.add(16);
l.add(23);
l.add(6);
System.out.println(“The minimum element from the collection: ”+Collections.min(list));
}
}

Output:

The maximum element from the collection: 5

Operations on Collections in Java:

1. Adding elements to the Collections class object in Java.

The add() method present in the java.util.Collection class lets us add all the particular elements to the collection. These elements can be either added individually or as an array.

Sample program to implement addition of elements to the collections class object:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class FirstCode{
public static void main(String[] args)
{
 List<String> items = new ArrayList<>();
items.add("Java");
items.add("C++");
Collections.addAll(items, "Python", "C", "JavaScript");
for (int i = 0; i < items.size(); i++) {
System.out.print(items.get(i) + " ");
}
}
}

Output:

Java C++ Python C JavaScript

2. Sorting a Java Collection:

The sorting methods of the java.util.Collections are used to sort the elements in a particular order. The java.util.Collection.sort() is used to sort the elements in ascending order. Whereas, the java.util.Collections.reverseOrder() sorts the list in the descending order.

Sample program to sort a collection:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class FirstCode{
public static void main(String[] args)
{
List<String> items = new ArrayList<>();
items.add("Java");items.add("C");
Collections.addAll(items, "Python", "JavaScript", "C++");
Collections.sort(items);
for (int i = 0; i < items.size(); i++) {
System.out.print(items.get(i) + " ");
}
System.out.println();
Collections.sort(items, Collections.reverseOrder());
for (int i = 0; i < items.size(); i++) {
System.out.print(items.get(i) + " ");
}
}
}

Output:

C C++ Java JavaScript Python

Python Java JavaScript C++ C

3. Searching in a Java collection

The method java.util.Collection.binarySearch() method returns the position of an object in a sorted list. This method expects the list to be present in the sorted order to provide an accurate result.

Sample program to implement searching in a collection:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FirstCode{
public static void main(String[] args)
{
List<String> items = new ArrayList<>();
items.add("Java");
items.add("C++");
items.add("C");
items.add("Python");
items.add("JavaScript");
Collections.sort(items);
System.out.println("The index of Python is "+ Collections.binarySearch(items, "Python"));
System.out.println("The index of C++ is "+ Collections.binarySearch(items, "C++"));
}
}

Output:

The index of Python is 3

The index of C++ is 1

4. Copying Elements in Java collection

The copy() method of java.util.Collections class copies all the elements from one list to another. Once the operation gets over, the index of each copied element in the destination list will be identical to the index in the source list. If the list is long, the remaining element is the destination list stay unaffected.

Sample program to implement copying elements:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class FirstCode{
public static void main(String[] args
{
List<String> destination_List = new ArrayList<>();
destination_List.add("Python");
destination_List.add("Java");
destination_List.add("C");
destination_List.add("C++");
System.out.println("The Original Destination list is ");
for (int i = 0; i < destination_List.size(); i++) {
}
System.out.println();
List<String> source_List = new ArrayList<>();
source_List.add("JavaScript");
source_List.add("ReactJS");
source_List.add("PHP");
Collections.copy(destination_List, source_List);
System.out.println("The Destination List After copying is ");
for (int i = 0; i < destination_List.size(); i++) {
System.out.print(destination_List.get(i) + " ");
}
}
}

Output:

The Original Destination list is Python Java C C++

The Destination List After copying is

JavaScript ReactJS PHP

5. Disjoint Collection in Java

The java.util.Collections.disjoint() method is used to check if the given collections are disjoint or not. Mostly, two collections are said to be disjoint when they have no elements in common. And this method returns true if there are no common elements in the set of collections.

Sample program to implement disjoint collection:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class GFG {
public static void main(String[] args)
{
List<String> list1 = new ArrayList<>();
list1.add("Java");
list1.add("C");
list1.add("C++");
list1.add("Python");
List<String> list2 = new ArrayList<>();
list2.add("JavaScript");
list2.add("ReactJS");
list2.add("PHP");
System.out.println(Collections.disjoint(list1, list2));
}
}

Output:

true

Conclusion:

This article would have helped you understand the Java Collections Class. you can implement the various methods to understand them clearly.

Leave a Reply

Your email address will not be published. Required fields are marked *