Java Collections

Why need Java Collections ?

As we know that the Arrays are static and their size does not grow with new insertion. We need a dynamic storage which grows automatically with new insertions and Java Collections are the answer to this problem. 

The most commonly used Interfaces are List, Set and Map. There are several implementations of those interfaces and in this tutorial we will discuss the similarities and differences between them. 

Many times, my students asked me which one is better and my answer to them is "Its all depend on the business requirement" there is nothing good or bad at face value. In this tutorial we will discuss different requirements and the use of Collection.

 

Java Collections tutorial

List Features

  • Unsorted
  • Duplicates are allowed

Set Features

  • Sorted
  • Duplicates are not allowed

Map Features

  • Key value pair
  • Sorted
  • Duplicate values are allowed but duplicate keys are not allowed

Lets discuss the classes implementing a List interface

ArrayList  

  • In ArrayList items are added at the end
  • It is good for quick insertions at the end but search is very slow because ArrayList is unsorted.
  • Insertion at specific index is allowed but slow down the process because there are no pointers or nodes in ArrayList. 

Vector

  • It is very similar to ArrayList and the only difference is that all the methods in the Vector are synchronised which means Vector is thread safe. Let me elaborate the concept of synchronised here. If method is synchronised then only one thread can access that method at a time. So, the lock is generated over the method and once the thread will leave the method then other thread will be able to access that method. So, no two threads can add or remove elements from the Vector at the same time. 
  • As Vector is thread safe, it is slow too because we can not take the advantage of multi-threading in Vector.
  • Only use Vector, when there is a serious requirement of security

LinkedList  

  • It is very similar to ArrayList but the difference is in speed. LinkedList manages links between indexes, so each insertion is slow as compare to ArrayList
  • As LinkedList manages links between each index, it is has constant time of insertion anywhere in the index.
  • If there is a requirement of insertion and deletion of an element anywhere or randomly then LinkedList is faster than  ArrayList.

How to create an ArrayList ?

There are several ways of creating an ArrayList. We can create a default ArrayList or a specific ArrayList.

List myFirstArrayList = new ArrayList();

or

List<Integer> myFirstArrayList  = new ArrayList<Integer>();

or

List<Employee> employees = new ArrayList<Employee>();

 

How to add an element in ArrayList ?

myFirstArrayList .add(10);

myFirstArrayList .add(12);

employees.add(new Employee("John");

employees.add(new Employee("David");

Lets discuss the classes implementing a Set interface

HashSet

  • HashSet stores the elements by using a mechanism called hashing.
  • HashSet contains unique elements only.

LinkedHashSet

  • LinkeHashSet is very similar to HashSet and the only difference is that the elements are linked to each other so the insertion and deletion happened at the constant time

TreeSet

  • Contains unique elements like HashSet.
  • Access and retrieval times are quiet fast.
  • Maintains ascending order

 

TreeSet in Java

TreeSet Structure

Creating , adding and deletion of elements in Set is same as List because they both extends Collection interface.

Lets discuss the classes implementing a Map interface

HashMap

  • HashMap is similar to HashSet and the only difference is HashMap stores key, value pair.

HashTable

  • HashTable is very similar to HashMap and the only difference is HashTable is synchronised and thread safe.
  • HashTable is slow because all the methods are synchronised.

TreeMap

  • TreeMap is similar to TreeSet and the only difference is TreeMap stores key, value pair.

How to create a TreeMap ?

There are several ways of creating a TreeMap. TreeMap hold key value pair like <key, value>. It can be any datatype of key and value. Duplicate values are allowed but duplicate keys are not allowed.

TreeMap myFirstTreeMap = new TreeMap();

or

TreeMap <Integer, String> myFirstTreeMap = new TreeMap <Integer, String> ();

 

How to add an element in TreeMap ?

myFirstTreeMap.put(1, "David");

myFirstTreeMap.put(2, "James");

// Get a set of the entries

Set set = myFirstTreeMap.entrySet();

// Get an iterator Iterator i = set.iterator();

// Display elements

while(i.hasNext()) {

    Map.Entry me = (Map.Entry)i.next();

    System.out.print(me.getKey() + ": ");

     System.out.println(me.getValue());

}


Output:

1 : David

2 : James