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.
- Duplicates are allowed
- Duplicates are not allowed
- Key value pair
- Duplicate values are allowed but duplicate keys are not allowed
Lets discuss the classes implementing a List interface
- 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.
- 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
- 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();
List<Integer> myFirstArrayList = new ArrayList<Integer>();
List<Employee> employees = new ArrayList<Employee>();
How to add an element in ArrayList ?
Lets discuss the classes implementing a Set interface
- HashSet stores the elements by using a mechanism called hashing.
- HashSet contains unique elements only.
- 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
- Contains unique elements like HashSet.
- Access and retrieval times are quiet fast.
- Maintains ascending order
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 is similar to HashSet and the only difference is HashMap stores key, value pair.
- 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 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();
TreeMap <Integer, String> myFirstTreeMap = new TreeMap <Integer, String> ();
How to add an element in TreeMap ?
// Get a set of the entries
Set set = myFirstTreeMap.entrySet();
// Get an iterator Iterator i = set.iterator();
// Display elements
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
1 : David
2 : James