Java Collections Utility Class
The Collections utility 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,
Some useful method in Collections class:
Method Signature Description
[Link](List myList) Sort the myList (implementation of any List interface)
provided an argument in natural ordering.
[Link](List, comparator c) Sort the myList(implementation of any List interface) as
per comparator c ordering (c class should implement
comparator interface)
[Link](List myList) Puts the elements of myList ((implementation of any List
interface)in random order
[Link](List myList) Reverses the elements of myList ((implementation of any
List interface)
[Link](List mlist, T key) Searches the mlist (implementation of any List interface)
for the specified object using the binary search algorithm.
[Link](List dest, List src) Copy the source List into dest List.
[Link](Collection c, Object o) Returns the number of elements in the specified
collection class c (which implements Collection interface
can be List, Set or Queue) equal to the specified object
[Link](Collection Returns a synchronized (thread-safe) collection backed
c) by the specified collection.
Let's take the example of List sorting using Collection class. We can sort any
Collection using “Collections” utility class. i.e.; ArrayList of Strings can be
sorted alphabetically using this utility class. ArrayList class itself is not
providing any methods to sort. We use Collections class static methods to do
this. Below program shows use of reverse(), shuffle(), frequency() methods as
well.
Java Code:
package utility;
import [Link];
import [Link];
import [Link];
public class CollectionsDemo {
public static void main(String[] args) {
List<String>student<String>List = new ArrayList();
[Link]("Neeraj");
[Link]("Mahesh");
[Link]("Armaan");
[Link]("Preeti");
[Link]("Sanjay");
[Link]("Neeraj");
[Link]("Zahir");
[Link]("Original List " + studentList);
[Link](studentList);
[Link]("Sorted alphabetically List " +
studentList);
[Link](studentList);
[Link]("Reverse List " + studentList);
[Link](studentList);
[Link]("Shuffled List " + studentList);
[Link]("Checking occurance of Neeraj: "
+ [Link](studentList,
"Neeraj"));
Copy
Output:
Using Collections class we can copy one type of collection to another type.
Collections provide us copy method to copy all the elements from source to
destination. Below program demonstrates the use of copy function. Here size
of source collection and destination collection should be same else we will get
following exception.
Java Code:Go to the editor
import [Link];
import [Link].*;
public class CopyListDemo {
public static void main(String[] args) {
List <Integer>myFirstList = new ArrayList<Integer>();
List <Integer> mySecondList = new
ArrayList<Integer>();
[Link](10);
[Link](20);
[Link](20);
[Link](50);
[Link](70);
[Link](11);
[Link](120);
[Link](120);
[Link](150);
[Link](170);
[Link]("First List-"+ myFirstList);
[Link]("Second List-"+ mySecondList);
[Link](mySecondList, myFirstList );
[Link]("Second List After Copy-"+
mySecondList);
}
Copy
Output: