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
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
- package myclass1;
- import java.util.*;
- class Myclass1 {
- public static void main(String args[]) {
- ArrayList AL = new ArrayList();
- AL.add("aman");
- AL.add("varun");
- AL.add("tarun");
- AL.add("raman");
- System.out.println("AL: " + AL);
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- Vector vec = new Vector(4, 3);
- vec.addElement(new Integer(21));
- vec.addElement(new Integer(22));
- vec.addElement(new Integer(23));
- Enumeration vecEnum = vec.elements();
- System.out.println("\nElements:");
- while (vecEnum.hasMoreElements())
- System.out.print(vecEnum.nextElement() + " ");
- System.out.println();
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- LinkedList LL = new LinkedList();
- LL.add("aman");
- LL.add("varun");
- LL.add("tarun");
- LL.add("raman");
- System.out.println("LL: " + LL);
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- HashSet TS = new HashSet();
- HS.add("a");
- HS.add("v");
- HS.add("tn");
- HS.add("ra");
- System.out.println("HS: " + HS);
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- TreeSet TS = new TreeSet();
- TS.add("amm");
- TS.add("vnn");
- TS.add("tnn");
- TS.add("ran");
- System.out.println("TS: " + TS);
- }
- }
Output
Linked Hash Set
It also contains unique elements. The insertion order of the elements is maintained in a Linked Hash Set.
Example
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- LinkedHashSet LHS = new LinkedHashSet();
- LHS.add("m");
- LHS.add("n");
- LHS.add("t");
- LHS.add("a");
- System.out.println("LHS: " + LHS);
- }
- }
Output
Queue
It is further divided into:
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- PriorityQueue qu = new PriorityQueue();
- qu.add("man");
- qu.add("nan");
- qu.add("tan");
- qu.add("aan");
- System.out.println("qu: " + qu);
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- Hashtable ht = new Hashtable();
- Enumeration name;
- String str;
- double bal;
- ht.put("ankur", new Double(1112.24));
- ht.put("Manu", new Double(1124.42));
- ht.put("Arun", new Double(1348.50));
- ht.put("Danik", new Double(9933.24));
- name = ht.keys();
- while (name.hasMoreElements())
- {
- str = (String) name.nextElement();
- System.out.println(str + ": " +
- ht.get(str));
- }
- System.out.println();
- bal = ((Double) ht.get("ankur")).doubleValue();
- ht.put("ankur", new Double(bal + 1000));
- System.out.println("ankur's new ht: " +
- ht.get("ankur"));
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- Map < String, String > hashMap = new HashMap < String, String > ();
- hashMap.put("Na", "four");
- hashMap.put("times", "5");
- hashMap.put("cost", "15000");
- System.out.println("Elements:");
- System.out.println(hashMap);
- }
- }
Output
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
- package myclass1;
- import java.util.*;
- class Myclass1
- {
- public static void main(String args[])
- {
- TreeMap tm = new TreeMap();
- tm.put("amyra", new Double(1314.134));
- tm.put("rahul", new Double(1221.221));
- tm.put("raj", new Double(1372.67));
- Set set = tm.entrySet();
- Iterator i = set.iterator();
- while (i.hasNext())
- {
- Map.Entry me = (Map.Entry) i.next();
- System.out.print(me.getKey() + ": ");
- System.out.println(me.getValue());
- }
- System.out.println();
- double balance = ((Double) tm.get("amyra")).doubleValue();
- tm.put("amyra", new Double(balance + 20000));
- System.out.println("amyra's new balance: " +
- tm.get("amyra"));
- }
- }
Output
Summary
This article explained collections and the various flavours of collections. These collections are very useful in Java.