java Collections class

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Collections class

Java collection class is used exclusively with static methods that operate on or return collections. It inherits Object class.

The important points about Java Collections class are:

  • Java Collection class supports the polymorphic algorithms that operate on collections.

  • Java Collection class throws a NullPointerException if the collections or class objects provided to them are null.


Collections class declaration

Let's see the declaration for Java.util.Collections class.

  1. public class Collections extends Object  


Methods of Java Collections class

Method

Description

static boolean addAll(Collection c, T... elements)

It is used to add all of the specified elements to the specified collection.

static Queue asLifoQueue(Deque deque)

It is used to return a view of a Deque as a Last-In-First-Out (LIFO) Queue.

static int binarySearch(List list, T key, Comparator

It is used to search the specified list for the specified object using the binary search algorithm.

static List checkedList(List list, Class type)

It is used to return a dynamically typesafe view of the specified list.

static Set checkedSet(Set s, Class type)

It is used to return a dynamically typesafe view of the specified set.

static SortedSetcheckedSortedSet(SortedSet s, Class type)

It is used to return a dynamically typesafe view of the specified sorted set

static void reverse(List list)

It is used to reverse the order of the elements in the specified list.

static T max(Collection coll, Comparator comp)

It is used to return the maximum element of the given collection, according to the order induced by the specified comparator.

static >T min(Collection coll)

It is used to return the minimum element of the given collection, according to the natural ordering of its elements.

static boolean replaceAll(List list, T oldVal, T newVal)

It is used to replace all occurrences of one specified value in a list with another.

Java Collections Example

  1. import java.util.*;  
  2. public class CollectionsExample {  
  3.     public static void main(String a[]){      
  4.         List<String> list = new ArrayList<String>();  
  5.         list.add("C");  
  6.         list.add("Core Java");  
  7.         list.add("Advance Java");  
  8.         System.out.println("Initial collection value:"+list);  
  9.         Collections.addAll(list, "Servlet","JSP");  
  10.         System.out.println("After adding elements collection value:"+list);  
  11.         String[] strArr = {"C#"".Net"};  
  12.         Collections.addAll(list, strArr);  
  13.         System.out.println("After adding array collection value:"+list);  
  14.     }  
  15. }  

Output:

Initial collection value:[C, Core Java, Advance Java]
After adding elements collection value:[C, Core Java, Advance Java, Servlet, JSP]
After adding array collection value:[C, Core Java, Advance Java, Servlet, JSP, C#, .Net]

Java Collections Example: max()

  1. import java.util.*;  
  2. public class CollectionsExample {  
  3.     public static void main(String a[]){         
  4.         List<Integer> list = new ArrayList<Integer>();  
  5.         list.add(46);  
  6.         list.add(67);  
  7.         list.add(24);  
  8.         list.add(16);  
  9.         list.add(8);  
  10.         list.add(12);  
  11.         System.out.println("Value of maximum element from the collection: "+Collections.max(list));  
  12.     }  
  13. }  

Output:

Value of maximum element from the collection: 67

Java Collections Example: min()

  1. import java.util.*;  
  2. public class CollectionsExample {  
  3.     public static void main(String a[]){         
  4.         List<Integer> list = new ArrayList<Integer>();  
  5.         list.add(46);  
  6.         list.add(67);  
  7.         list.add(24);  
  8.         list.add(16);  
  9.         list.add(8);  
  10.         list.add(12);  
  11.         System.out.println("Value of minimum element from the collection: "+Collections.min(list));  
  12.     }  
  13. }  

Output:

Value of minimum element from the collection: 8