Java Collections Framework

The Java Collections framework has reduced the cumbersome work of actually creating data structures like List, Set, Map. The basic idea behind Collections is that they are used to add, delete, search, sort and iterate over objects. This post will seek to explain the Java Collections framework.

Is it “Collection” or “Collections”?

At the outset, the word “Collection” in Java might be misunderstood for other terms. It is always important to note if it is ‘Collection’ or ‘Collections’. We have ‘java.util.Collection’ which is an interface and which is our primary point of discussion in this post. We also have’java.util.Collections’ which is a class with most static methods such as addAll, checkedListand so on.   According to the Oracle website, the Java Collections framework consists of interfaces, its implementations and algorithms.

Some of the different sub- interfaces under the Collection interface are Set, List, Queue. Some of the important implementing classes of List are LinkedList, ArrayList, and Vector. Let us see the Vector class and LinkedList class before moving onto the ArrayList.

Vector class:

For those of us working with Java from its earlier days in the late 90s, the Vector class is a familiar name. The Vector class is similar to an array and can hold elements and grow as needed. It is synchronized and has been transformed to the new ‘ArrayList’ class. If a thread safe environment is not needed, the ArrayList class is recommended to be used.

LinkedList class:

LinkedList is again another fundamental data structure that has been implemented in Java collections. Since the linked list is a doubly linked list rather than a single linked one, there are more methods for adding elements towards the end or the beginning. Adding or deleting elements to a doubly linked list is easier than iterating over it.

ArrayList class:

As already stated, the ArrayList is also an array but which can grow on demand. It is unsynchronized and perfect for situations when the performance overhead of thread safety is unnecessary.

The new ArrayList class is much more powerful for Collection operations like insertion and searching. An ArrayList is used to store objects of different type and can be used with Generics. The following program is a simple illustration of the ArrayList class:

package collection;
import java.util.*;
public class col_1 {
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           List<Integer> a1=new ArrayList<Integer>();
           a1.add(2);
           a1.add(45);
     for(Integer a2:a1){
     System.out.println(a2);
     }
     a1.set(1, 25);// The number at 1st position is set to '25'
     a1.add(8);
     System.out.println("The list now is" );
     for(Integer a2:a1){
            System.out.println(a2);
    }
     Collections.sort(a1);
     System.out.println("The list after sorting is" );
     for(Integer a2:a1){
      System.out.println(a2);
     }
}
}

The above program is initially used to create an ArrayList of Integer objects. Two numbers are added to it, which are printed out.  The number at the 1st position is modified and a 3rd number is added to it thereby emphasizing the dynamic growth of the ArrayList.

Once there are three numbers in the list, they are sorted according to the natural order by specifying the method from the java.util.Collections class

Collections.sort(a1);

The output of the program is as follows:

2
45

The list now is

2
25
8

The list after sorting is

2
8
25

Generics and Collections:

J2SE 5 introduced ‘Generics’ which enabled Collections to store objects of similar type. This reduced the effort and errors caused by type casting.

Prior to Java 5, an ArrayList had to be created as follows:

List a1=new ArrayList();
  a1.add(2);
  int i=(int)a1.get(0); // type casting needed to retrieve elements
 System.out.println(i);

By means of Generics, the ArrayList class can be created as follows:

List<String> a =new ArrayList<String>();
     a.add("abc");
System.out.println(a.get(0)); // type casting not needed

Generics makes use of  “<>” brackets to specify what type of objects an ArrayList can store. In the above example, the ArrayList is used to store ‘String’ objects.

We have seen Java Collections and some important classes relating to the List interface in this post. We will explore more of Collections in subsequent posts.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

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

Scroll to Top