Java Collections Class

java collections class

Java Collections Class

Java Collections class is a utility class present in java.util package. This class consist exclusively of static methods that operate on collections object or returns collections(group of elements). Because it is a utility class , it contains polymorphic algorithms that operate on collections wrappers which returns a new collection backed by a specific collection. 

The methods of this class all throws a NullPointerException if the collections or else class objects provides to them are null. Java collections class has destructive algorithms seems like algorithms that modify the collection which they operate are specified to throw UnSupportedOperationException if the current collection does not support the appropriate mutation primitives such as ‘set’ method.

Therefore the Collections utility class is mostly used to add synchronization to many unsynchronized collections. 

Difference between ‘synchronized’ and ‘unsynchronized’ collections

Unsynchronized collections 

  • unsynchronized collections are not thread safety as a result any number of threads can access one single collections at a time.
  • Example : ArrayList collections are unsynchronized collections.

Synchronized collections

  • Synchronized collections are the collections where only one thread is allowed to access that collection.
  • After the first thread completes it’s operations , then the second thread will access the collections and so on.
  • Example  : Vector collections are synchronized collections.

Conversion of Unsynchronized collections to Synchronized version

To convert this , we need to make use of Collections utility class. Suppose there is an unsynchronized collection called ‘mycollection‘. And this mycollection is String type (which contains only string elements). Now we have to convert it into synchronized version with the help of Java Collections class. 

Collection c = Collections.SynchronizedCollection(mycollection);

Synchronized(c)
{
    Iterator itr = c.iterator(); // must be in synchronized block
    while(itr.hasNext())
     {
          foo(itr.next);
     }
}

As a result , now collection ‘c’ is synchronized collection.

What is meant by fail-fast and fail-safe iterator?

Let’s take an example of collection ‘c’ which has int elements from 1 t 10. In order to iterate those elements we have to use one of the iterator methods. listIterator is the best iterator to traverse the elements. listIterator methods iterated from  to 1 to 10 and the returned list is fail-fast .

Fail-fast iterator

When a problem occurs , a fail-fast system fails immediately , in other words when you call a iterator on a collection ‘c’ and another thread tries to modify the same collection ‘c’ then it throws ConcurrentModificationException , this is called fail-fast.

Fail-fast iterator comes to know the modification with the help of “mod” flag. If the value of mod flag changes then it throws ConcurrentModificationException.

Fail-safe iterator

Fail-safe iterator makes a copy of the internal data structure(array or collection) and iterates on that copied array or collection. So any structural modification done to the iterator affects the copied data structure so original data structure remains structurally unchained. No ConcurrentModificationException is thrown.

Differences between Java’s Collections class and Collection interface

Collection Interface 

  • Collection is an root interface for the Java Collection Framework ,  it provides basic optional and core methods to operate on collections objects.
  • As well this  interface does not have any direct implementation classes but it does have sub-interfaces like Set , List , Queue.
  • Map interface does not belong to  Collection interface , Collection interface is a member of java.util package.

Collections class

  • Java Collections class is a utility class , present in java.util package.
  • Java Collections class consists of exclusively only static methods that operates on collections objects.
  • Collections class is also used to convert unsynchronized to synchronize collections.
  • It has methods like finding the maximum and minimum element present in the collection and also it has sorting method to sort the collection and many more. 

Some of the main Collections utility class methods are shown below

 Index                                Methods
 1  disjoint(Collection c1 , Collection c2)

public static boolean disjoint(Collection c1 , Collection c2)

  • Returns true if the two specified collections have no elements in common.
  •  while using this method , both the specified collections must use the same nonstandard equality test , or else the result of this method is undefined .
  • c1 –> collection 1 
  • c2 –> collection 2 

Exceptions 

Throws

  1. NullPointerException : If  either the collections contains the null elements.
  2. ClassCastException : If the element in c1 collection is of String type which is not eligible to the other collection c2 then it throws this exception

 2  min()

public static <T extends Object & Comparable> T min(Collection c1)

  • Returns the minimum element in the specified collection ‘c1’ .
  • To execute this method , all the elements present in the c1 collection must implement Comparable interface.
  • In other words , all the elements in the collection must be manually comparable (e1.compareTo(e2)) must not throw any ClassCastException. Where e1 and e2 are elements.
  • This method iterates over the entire collection.

Exceptions 

Throws

  1. NullPointerException :If the Collection is empty.
  2. ClassCastException :If the collections contains elements that are not mutually comparable for example Strings and integers.

 3  max()

public static <T extends Object & Comparable > T max(Collection c1)

  • Returns the maximum element in the specified collection ‘c1’ .
  • To execute this method , all the elements present in the c1 collection must implement Comparable interface.
  • In other words , all the elements in the collection must be manually comparable (e1.compareTo(e2)) must not throw any ClassCastException. Where e1 and e2 are elements.
  • This method iterates over the entire collection.

Exceptions 

Throws

  1. NullPointerException :If the Collection is empty.
  2. ClassCastException :If the collections contains elements that are not mutually comparable for example Strings and integers.

 

 

 

5 thoughts on “Java Collections Class

  1. The very crux of your writing while appearing agreeable originally, did not settle very well with me after some time. Somewhere throughout the sentences you were able to make me a believer unfortunately only for a short while. I nevertheless have a problem with your leaps in assumptions and you might do well to fill in those breaks. In the event you can accomplish that, I would undoubtedly be impressed.

  2. I just want to tell you that I am just new to weblog and honestly savored this web blog. Very likely I’m planning to bookmark your site . You definitely have impressive stories. Bless you for sharing your web-site.

  3. I simply want to mention I’m new to blogging and site-building and absolutely enjoyed this blog site. Almost certainly I’m want to bookmark your site . You certainly come with beneficial articles and reviews. Cheers for sharing your web-site.

Leave a Reply

Your email address will not be published. Required fields are marked *