Collections in Java

Collections in Java

The collection as you know a group, unit or multiple values that are collection. But in Java programming what is this? Collections package is a very important role in java programming. Every developer needs this to minimize, optimize his code with the help of collections.

A Collection is a group of specific objects which are described in a single unit.Java language presents collection framework which defines various classes and interfaces to represent a combination of objects as a single unit.

The Collection interface has two root interface. 

 One is java.util.Collection and another is Map interface (java.util.Map). They are two main root interfaces of Java collection classes.

 

Why we need Collection Framework :


Before Collection Framework and the version of JDK 1.2 was introduced, the standard methods for grouping Java objects (or collections) were an array or Vector or Hashtable. All three of these collections had no common interface and no common methods.
Suppose we want to access elements an array, vector or Hashtable. All these three have distinct methods and syntax for accessing members:

 

// Java program to show why collection framework was needed

import java.io.*;
import java.util.*;

class Test{
    public static void main (String[] args){

        // Creating instances of array, vector and hashtable

        int arr[] = new int[] {1, 2, 3, 4}; 
        Vector<Integer> v = new Vector(); 
        Hashtable<Integer, String> h = new Hashtable();

        v.addElement(1);
        v.addElement(2); 
        h.put(1,"RAM");
        h.put(2,"SOHAN");

        // Array instance creation requires [], while Vector and hastable require ()
        // Vector element insertion requires addElement(),but hashtable element insertion requires put() 
        // Accessing first element of array, vector and hashtable

        System.out.println(arr[0]);
        System.out.println(v.elementAt(0));
        System.out.println(h.get(1));

        // Array elements are accessed using [], vector elements

        // using elementAt() and hashtable elements using get()

    }

}

 

Output:

1
1
RAM

As we can see above example, none of the collections Array, Vector or Hashtable implements a standard member access interface. That why it was very difficult for Java Developers to write an algorithm that can work for all kind of collections.
Another disadvantage is that most of the ‘Vector’ methods are final. That's  why we cannot extend ’Vector’ class to implement a similar kind of collection.
Now the Java developers decided to come up with new and some common interface to deal with the above-specified problems and introduced Collection Framework. They presented collections in JDK 1.2 version and changed the legacy Vector and Hashtable to conform to the collection framework.

 

Advantages of Collection Framework:

 

  1. Compatible API : The API has the basic and easy set of interfaces. In Collection, Set, List, or Map. All these classes (such as ArrayList, LinkedList, Vector etc) which implement, these interfaces have some common set of methods.
  2. Decreases programming effort: As I told you Collections framework reduce the programming effort. Java Developer need not worry about design of Collection rather than he can focus on its best use in his program.
  3. Increases program speed and quality: Increases performance, production by providing high-performance implementations of very useful data structures and algorithms.

 

Hierarchy of Collection Framework

 

             Collection                Map
         /     /    \      \            |
        /      /      \     \           |
     Set    List    Queue  Dequeue   SortedMap
     /
    /
 SortedSet 
            Core Interfaces in Collections

Note that this diagram shows only core interfaces.  
Collection : Root interface with basic methods like add(), remove(), 
             contains(), isEmpty(), addAll(), ... etc.
 
Set : Doesn't allow duplicates. Example implementations of Set 
      interface are HashSet (Hashing based) and TreeSet (balanced
      BST based). Note that TreeSet implements SortedSet.

List : Can contain duplicates and elements are ordered. Example
       implementations are LinkedList (linked list based) and
       ArrayList (dynamic array based)

Queue : Typically order elements in FIFO order except exceptions
        like PriorityQueue.  

Deque : Elements can be inserted and removed at both ends. Allows
        both LIFO and FIFO. 

Map : Contains Key value pairs. Doesn't allow duplicates.  Example
      implementation are HashMap and TreeMap. 
      TreeMap implements SortedMap.        

The difference between Set and Map interface is, in Set we have only
keys, but in Map, we have key value pairs.

collectionjava

Related Articles

post a comment