Collections in Java

Introduction

 
Collections represent a group. In arrays there is a size restriction, but in collections there is no size restriction. It grows and shrinks at run time. This is the main advantage of collections. The following are the four flavours of collections.
  • List
  • Set
  • Queue
  • Map

List

 
It is further divided into :
  • Array List
  • Vector (oldest list provided by Java)
  • Linked List

Array List

 
As far as Array Lists are concerned, they can grow or shrink, that's why they are different from arrays. Array lists are initialized by an initial size, but if we need more or less size then it will automatically grow or shrink accordingly. So if you do not know the size then it is better to use an Array List.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1 {  
  4.     public static void main(String args[]) {  
  5.         ArrayList AL = new ArrayList();  
  6.         AL.add("aman");  
  7.         AL.add("varun");  
  8.         AL.add("tarun");  
  9.         AL.add("raman");  
  10.         System.out.println("AL: " + AL);  
  11.     }  
  12. }  
Output
 
Array List in Java 
 

Vector

 
As far as vector is considered, it implements an array, but that array is dynamic. It is the oldest list provided by Java. It is similar to Array Lists, but has some differences. It is synchronized, on the other hand Array List is not.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         Vector vec = new Vector(43);  
  8.         vec.addElement(new Integer(21));  
  9.         vec.addElement(new Integer(22));  
  10.         vec.addElement(new Integer(23));  
  11.         Enumeration vecEnum = vec.elements();  
  12.         System.out.println("\nElements:");  
  13.         while (vecEnum.hasMoreElements())  
  14.             System.out.print(vecEnum.nextElement() + " ");  
  15.         System.out.println();  
  16.     }  
  17. }  
Output
 
Java Vector 
 

Linked List

 
This is also an implementation of a list. Like Array List, it is also not synchronized. It has a contiguous memory allocation. It allows duplicate values.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         LinkedList LL = new LinkedList();  
  8.         LL.add("aman");  
  9.         LL.add("varun");  
  10.         LL.add("tarun");  
  11.         LL.add("raman");  
  12.         System.out.println("LL: " + LL);  
  13.     }  
  14. }  
Output
 
linked list in Java 
 

Set

 
It is further divided into:
  • Hash Set
  • Tree Set
  • Linked Hash Set

Hash Set

 
It uses Hash Table to store the elements. It contains only unique elements. The process of storing elements is known as hashing.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         HashSet TS = new HashSet();  
  8.         HS.add("a");  
  9.         HS.add("v");  
  10.         HS.add("tn");  
  11.         HS.add("ra");  
  12.         System.out.println("HS: " + HS);  
  13.     }  
  14. }  
Output
 
hash set in Java 
 

Tree Set

 
It also contains unique elements. The ascending order of elements is maintained in a Tree Set, hence it's access and retrieval time is very fast, that's why it is very useful.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         TreeSet TS = new TreeSet();  
  8.         TS.add("amm");  
  9.         TS.add("vnn");  
  10.         TS.add("tnn");  
  11.         TS.add("ran");  
  12.         System.out.println("TS: " + TS);  
  13.     }  
  14. }  
Output
 
tree set in Java 
 

Linked Hash Set

 
It also contains unique elements. The insertion order of the elements is maintained in a Linked Hash Set.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         LinkedHashSet LHS = new LinkedHashSet();  
  8.         LHS.add("m");  
  9.         LHS.add("n");  
  10.         LHS.add("t");  
  11.         LHS.add("a");  
  12.         System.out.println("LHS: " + LHS);  
  13.     }  
  14. }  
Output
 
linked hash set in Java 
 

Queue

 
It is further divided into:
  • Priority Queue

Priority Queue

 
It uses a queue, but like a queue its elements are not in FIFO (first in first out) order. The elements are ordered naturally depending on their order. It does not allow null elements.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         PriorityQueue qu = new PriorityQueue();  
  8.         qu.add("man");  
  9.         qu.add("nan");  
  10.         qu.add("tan");  
  11.         qu.add("aan");  
  12.         System.out.println("qu: " + qu);  
  13.     }  
  14. }  
Output
 
priority queue in Java 
 

Map

 
It is further divided into:
  • Hash Table
  • Hash Map
  • Tree Map

Hash Table

 
We have a key and a value in a hash table. It cannot contain any null key or value. It also contains unique elements. It is also synchronized.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         Hashtable ht = new Hashtable();  
  8.         Enumeration name;  
  9.         String str;  
  10.         double bal;  
  11.         ht.put("ankur"new Double(1112.24));  
  12.         ht.put("Manu"new Double(1124.42));  
  13.         ht.put("Arun"new Double(1348.50));  
  14.         ht.put("Danik"new Double(9933.24));  
  15.         name = ht.keys();  
  16.         while (name.hasMoreElements())  
  17.         {  
  18.             str = (String) name.nextElement();  
  19.             System.out.println(str + ": " +  
  20.                 ht.get(str));  
  21.         }  
  22.         System.out.println();  
  23.         bal = ((Double) ht.get("ankur")).doubleValue();  
  24.         ht.put("ankur"new Double(bal + 1000));  
  25.         System.out.println("ankur's new ht: " +  
  26.             ht.get("ankur"));  
  27.     }  
  28. }  
Output
 
hash table in Java 
 

Hash Map

 
Unlike a Hash Table it can have one null key and many null values. No order is maintained. It is completely based on the key.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         Map < String, String > hashMap = new HashMap < String, String > ();  
  8.         hashMap.put("Na""four");  
  9.         hashMap.put("times""5");  
  10.         hashMap.put("cost""15000");  
  11.         System.out.println("Elements:");  
  12.         System.out.println(hashMap);  
  13.     }  
  14. }  
Output
 
hash map in Java 
 

Tree Map

 
It is also based on the key. Unlike a Hash Map it cannot have a null key, but can have many null values. Ascending order is maintained in a Tree Map. Retrieval is very fast here.
 
Example
  1. package myclass1;  
  2. import java.util.*;  
  3. class Myclass1  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         TreeMap tm = new TreeMap();  
  8.         tm.put("amyra"new Double(1314.134));  
  9.         tm.put("rahul"new Double(1221.221));  
  10.         tm.put("raj"new Double(1372.67));  
  11.         Set set = tm.entrySet();  
  12.         Iterator i = set.iterator();  
  13.         while (i.hasNext())  
  14.         {  
  15.             Map.Entry me = (Map.Entry) i.next();  
  16.             System.out.print(me.getKey() + ": ");  
  17.             System.out.println(me.getValue());  
  18.         }  
  19.         System.out.println();  
  20.         double balance = ((Double) tm.get("amyra")).doubleValue();  
  21.         tm.put("amyra"new Double(balance + 20000));  
  22.         System.out.println("amyra's new balance: " +  
  23.             tm.get("amyra"));  
  24.     }  
  25. }  
Output
 
tree map in Java 
 

Summary

 
This article explained collections and the various flavours of collections. These collections are very useful in Java.


Similar Articles