Java.util.Collections Class API Guide
Java.util.Collections Class API Guide
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.
In this post, we will explore few useful Collections class methods with examples
java.util.Collections methods
•sort(List list)
•sort(List list, Comparator<? super Project> c)
•shuffle(List<?> list)
•reverse(List<?> list)
•rotate(List<?> list, int distance)
•swap(List<?> list, int i, int j)
•replaceAll(List list, String oldVal, String newVal)
•copy(List<? super String> dest, List<? extends String> src)
•Collections.binarySearch(list, "element 4")
•frequency(Collection<?> c, Object o)
•disjoint(Collection> c1, Collection> c2)
•min(Collection extends ?> coll)
•max(Collection extends ?> coll)
java.util.Collections Class
The Collections class provides static methods whose first argument is the collection on which the
operation is to be performed The great majority of the algorithms provided by the Java platform
operate on List instances, but a few of them operate on arbitrary Collection instances. We use
Collections class to demonstrate below Algorithmswith examples:
•Sorting
•Shuffling
•Routine Data Manipulation
•Searching
•Composition
•Finding Extreme Values
Sorting using java.util.Collections Class
The Collections class provides two sorting methods:
sort(List list)
Sorts the specified list into ascending order, according to the natural ordering of its elements. All
elements in the list must implement the Comparable interface.
Output:
sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@Override
public int compareTo(Project project) {
return this.getProjectId() - project.getProjectId();
}
}
Output:
200
APEX
50
CMS
100
TMS
shuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness. All permutations occur
with approximately equal likelihood.
Example:
Collections.sort(list);
for (String str : list) {
System.out.println(" sort elements in ascending order --" + str);
}
Output:
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
shuffle elements --element 1
shuffle elements --element 3
shuffle elements --element 4
shuffle elements --element 2
Collections.sort(list);
for(String str : list){
System.out.println(" sort elements in ascending order --" + str);
}
//reverses the order of the elements in a List.
Collections.reverse(list);
Collections.sort(list);
for (String str : list) {
System.out.println(" sort elements in ascending order --" + str);
}
int index = Collections.binarySearch(list, "element 4");
System.out.println("Element found at ::" + index);
}
Output:
sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
Element found at ::3
Composition
The frequency and disjoint algorithms test some aspect of the composition of one or more
Collections:
•frequency - counts the number of times the specified element occurs in the specified
collection.
•disjoint - determines whether two Collections are disjoint; that is, whether they contain
no elements in common.
frequency(Collection<?> c, Object o)
Returns the number of elements in the specified collection equal to the specified object. More
formally, returns the number of elements e in the collection such that (o == null ? e == null :
o.equals(e)).
Example: