Java provides a framework called the "Collection Framework," which contains all the collection classes and interfaces. A collection in Java refers to a group of individual objects that are treated as a single unit. Understanding the Collection class in Java is essential as it offers a wide range of methods that are helpful in manipulating collections. In this article, we will explore the Collection class and its methods.
What is the Collection Class in Java?
In Java, the Collection class is a part of the Collection Framework that contains all the methods to operate on collections of objects. It is an abstract class and serves as a root for all the collection classes. The Collection interface and the Map interface are the two main "root" interfaces of Java Collection classes. All the methods present in the collection class are static, thus saving extra effort by eliminating the need for creating another instance to call those methods.
Collection Methods
The Collection class contains several methods that are helpful in manipulating collections. Here are some of the most important methods:
- min(C): Returns the minimum element of the given collection, according to the natural ordering of its elements.
- min(Collections, Comparator): Returns the minimum element of the given collection, according to the order induced by the specified comparator.
- max(C): Returns the maximum element of the given collection, according to the natural ordering of its elements.
- max(Collections, Comparator): Returns the maximum element of the given collection, according to the order induced by the specified comparator.
- disjoint(c1, c2): Checks whether two specified collections are disjoint or not. More formally, two collections are disjoint if they have no elements in common. This returns true if the two specified collections have no elements in common.
- frequency(c, Obj): Returns the frequency of an object Obj present in the specified list of Collection c. More formally, it returns the number of elements Obj in the collection.
List Methods
The List interface extends the Collection interface and provides several additional methods. Here are some of the most important List methods:
- binarySearch(l, key): Returns the position of a key in a sorted list l. If the key is not present, it returns "-(insertion_point + 1)". The insertion point is defined as the point at which the key would be inserted into the list. It has two versions, one to implement the Comparable interface and the other to implement a Comparator interface.
- copy(dest, src): Copies all the elements from the source list(src) to a destination list(dest). The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.
- fill(l, obj): Replaces all the elements of the specified list(l) with the specified object(obj).
- indexOfSubList(srcL, targetL): 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.
- lastIndexOfSubList(srcL, targetL): Returns the last position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
- replaceAll(l, old, new): Replaces all occurrences of an old value in a list with another new value.
- reverse(): Reverses all the elements in a list.
- rotate(l, dist): Rotates the elements present in the specified list of Collection by a given distance in a clockwise manner.
- sort(l): Sorts the given list. It has two versions, one to implement the Comparable interface and the other to implement a Comparator interface.
- swap(l, i, j): Swaps the element present in position i and j in a specified list l.
Other Methods
The Collection class contains several other methods besides the ones mentioned above. Here are some additional methods:
addAll(Collection<? super T> c, T... elements): This method is used to add multiple elements to a collection at once. The elements to be added are specified as varargs.
asLifoQueue(Deque<T> deque): This method returns a view of a Deque as a Last-In-First-Out (LIFO) Queue.
emptyList(): This method returns an empty list (immutable). This is often used to initialize an empty list.
emptySet(): This method returns an empty set (immutable). This is often used to initialize an empty set.
singleton(T obj): This method returns an immutable set containing only the specified object.
disjoint(Collection<?> c1, Collection<?> c2): This method is used to check whether two specified collections are disjoint or not. Two collections are disjoint if they have no elements in common.
fill(List<? super T> list, T obj): This method replaces all the elements of a list with the specified object.
frequency(Collection<?> c, Object o): This method returns the number of occurrences of a specified object in a collection.
indexOfSubList(List<?> source, List<?> target): This method returns the index of the first occurrence of a specified sublist within a specified list.
lastIndexOfSubList(List<?> source, List<?> target): This method returns the index of the last occurrence of a specified sublist within a specified list.
nCopies(int n, T o): This method returns an immutable list consisting of n copies of the specified object.
replaceAll(List<T> list, T oldVal, T newVal): This method replaces all occurrences of a specified value in a list with another specified value.
reverseOrder(): This method returns a comparator that imposes the reverse ordering of the natural ordering.
shuffle(List<?> list): This method randomly shuffles the elements of a list.
swap(List<?> list, int i, int j): This method swaps the elements at the specified positions in a list.
Let's see an example of using the Collection class and some of its methods:
javaimport java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class CollectionExample {
public static void main(String[] args) {
// Creating an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<Integer>();
// Adding some elements to the ArrayList
numbers.add(5);
numbers.add(2);
numbers.add(10);
numbers.add(8);
// Finding the minimum element in the collection
System.out.println("Minimum element in the collection: " + Collections.min(numbers));
// Finding the maximum element in the collection
System.out.println("Maximum element in the collection: " + Collections.max(numbers));
// Checking if two collections are disjoint
ArrayList<Integer> anotherList = new ArrayList<Integer>();
anotherList.add(15);
anotherList.add(20);
System.out.println("Are the two collections disjoint? " + Collections.disjoint(numbers, anotherList));
// Finding the frequency of an element in the collection
System.out.println("Frequency of element 5 in the collection: " + Collections.frequency(numbers, 5));
// Sorting the collection
Collections.sort(numbers);
System.out.println("Sorted collection: " + numbers);
}
}
In this example, we first create an ArrayList
of integers and add some elements to it. Then we use various methods of the Collections
class to find the minimum and maximum elements in the collection, check if it is disjoint with another collection, find the frequency of an element in the collection, and sort the collection in ascending order.
Output:
yamlMinimum element in the collection: 2
Maximum element in the collection: 10
Are the two collections disjoint? true
Frequency of element 5 in the collection: 1
Sorted collection: [2, 5, 8, 10]
As you can see, the Collections
class provides many useful methods for working with collections in Java.
Collection class in Java provides a wide range of useful methods for working with collections of objects. By using these methods, you can easily manipulate collections and perform various operations on them.