Introduction:
A collection of individual objects represented as a single unit is known as a collection of objects. In Java, the "Collection Framework" is a separate framework defined in JDK 1.2 that holds all the collection classes and interfaces. The Collection interface (java.util.Collection) and the Map interface (java.util.Map) are the two main root interfaces of Java Collection classes.
Collection Methods:
The collection methods are applied to things that implement the Collection interface.- min(C): This returns the minimum element of the given collection, according to the natural ordering of its elements.
- min(Collections, Comparator): This returns the minimum element of the given collection, according to the order induced by the specified comparator.
- max(C): This returns the maximum element of the given collection, according to the natural ordering of its elements.
- max(Collections, Comparator): This returns the maximum element of the given collection, according to the order induced by the specified comparator.
- disjoint(c1, c2): This 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): This 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 methods are applied to things that implement the List interface.- binarySearch(l, key): This method 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): This is used to copy all of 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): This replaces all of the elements of the specified list (l) with the specified object (obj).
- indexOfSubList(srcL, targetL): This 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. More formally, it returns the lowest index i such that source.subList(i, i+target.size()).equals(target), or -1 if there is no such index (returns -1 if target.size() > source.size()).
- lastIndexOfSubList(srcL, targetL): This 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): This replaces all occurrences of an old value in a list with a new value.
- reverse(): This simply reverses all the elements in a list.
- rotate(l, dist): This rotates the elements present in the specified list of Collection by a given distance in a clockwise manner.
- sort(l): This 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): This swaps the elements present in position i and j in a specified list (l).
Other Methods:
These methods are used to modify and access Java collections in different ways.- reverseOrder(): This method reverses the natural ordering. We can provide a Comparator that imposes reverse order of a passed Comparator object. We can use this method to sort a collection in descending order.
Here's an example of how to use the reverseOrder()
method to sort a collection in reverse order:
javaList<Integer> numbers = Arrays.asList(5, 2, 8, 1, 6);
Collections.sort(numbers, Collections.reverseOrder());
System.out.println(numbers);
Output:
java[8, 6, 5, 2, 1]
In this example, we have a list of integers and we want to sort them in descending order. We first call the sort()
method from the Collections
class, passing in the list of integers and the reverseOrder()
method. This method returns a Comparator
object that imposes the reverse natural ordering of the elements in the collection. The sort()
method then uses this comparator to sort the elements in reverse order.
Note that we can also provide a custom Comparator object that imposes a specific ordering on the elements. In this case, the reverseOrder
method will reverse that ordering, effectively sorting the elements in reverse order of the custom ordering. Here's an example:
javaList<Integer> numbers = Arrays.asList(5, 2, 7, 1, 8, 4);
Comparator<Integer> customComparator = (a, b) -> a.compareTo(b); // ascending order
numbers.sort(Comparator.reverseOrder(customComparator)); // sort in descending order
System.out.println(numbers);
Output:
java[8, 7, 5, 4, 2, 1]
In this example, we created a list of integers and a custom Comparator that imposes ascending order on the integers. We then used the reverseOrder
method to reverse that ordering, effectively sorting the integers in descending order. Finally, we printed the sorted list, which shows the integers in descending order.