Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 23 March 2013

Java collection framework - Set, List and Map Interfaces in Java

In this particular blog we will talk about a very useful and important part of Java language i.e. Collection framework. Collection framework provides interfaces and class implementations that enable data handling easy and meaningful. Using collections one can store, retrieve and manipulate the data very effectively and easily.
Before we start lets first discuss the Architecture of Collection framework and hierarchy of Classes and Interfaces.


Collection Interface

The Collection Interface resides at the top of the Hierarchy, although Java does not provides a direct implementation of Collection framework but Collection Interface is being implemented by List and Set Classes.

Set Interface

A set is a Interface that does not contain duplicate values. In provides three general purpose implementations in Java.

1) HashSet
2) TreeSet
3) LinkedHashSet


1) HashSet

HashSet is the best performing implementation of Set interface. It stores its elements in a HashTable an does not guarantee of any type of ordering in iteration.
package com.beingjavaguys.core;
import java.util.HashSet;

public class HashSetImplementation {

    public static void main(String args[]){

        HashSet<String> hashSet = new HashSet<String>();

        hashSet.add("element1");
        hashSet.add("element2");
        System.out.println(hashSet);

        hashSet.remove("element1");
        System.out.println(hashSet);
    }
}


2) TreeSet

TreeSet is a little slow than HashSet and it stores its elements in a red-black tree structure. TreeSet orders its elements on the basis of their values.
package com.beingjavaguys.core;
import java.util.TreeSet;

public class TreeSetImplementation {

    public static void main(String args[]){

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("element1");
        treeSet.add("element2");
        treeSet.add("element3");
        System.out.println(treeSet);

        treeSet.remove("element2");
        System.out.println(treeSet);
    }    
}


3) LinkedHashSet

LinkedHashSet is implemented as a HashTable with a LinkedList running through it. It orders its elements on the basis of order in which they were inserted in the set.
package com.beingjavaguys.core;
import java.util.LinkedHashSet;

public class LinkedHashSetImplementation {

    public static void main(String[] args) {

        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();

        linkedHashSet.add("element1");
        linkedHashSet.add("element2");
        linkedHashSet.add("element3");
        System.out.println(linkedHashSet);

        linkedHashSet.remove("element1");
        System.out.println(linkedHashSet);
    }
}


List Interface

A List is a ordered collection that can contain duplicate values. It provides three general purpose implementations.

1) ArrayList
2) LinkedList
3) Vector

1) ArrayList

ArrayList is said to be the best performing list implementation in normal conditions. In simple words we can say that ArrayList is a expendable array of values or objects.
package com.beingjavaguys.core;
import java.util.ArrayList;

public class ArrayListImplementation {

    public static void main(String args[]){

        ArrayList<String> arrayList = new ArrayList<String>();

        arrayList.add("element1");
        arrayList.add("element2");
        arrayList.add("element3");
        System.out.println(arrayList);

        arrayList.remove("element3");
        System.out.println(arrayList);
    }
}


2) LinkedList

Linked list is a bit slower than ArrayList but it performs better in certain conditions.
package com.beingjavaguys.core;
import java.util.LinkedList;

public class LinkedListImplementation {

    public static void main(String args[]){
        LinkedList<String> linkedList = new LinkedList<String>();

        linkedList.add("element1");
        linkedList.add("element2");
        linkedList.add("element3");
        System.out.println(linkedList);

        linkedList.remove("element3");
        System.out.println(linkedList);
    }
}


3) Vector

Vector is also a growable array of objects, but unlike ArrayList Vector is thread safe in nature.
package com.beingjavaguys.core;
import java.util.Vector;

public class VectorImplementation {

    public static void main(String args[]){

        Vector<String> vector = new Vector<String>();

        vector.add("element1");
        vector.add("element2");
        vector.add("element3");
        System.out.println(vector);

        vector.remove("element1");
        System.out.println(vector);
    }
}


Queue Interface

A Queue interface provides implementation to hold elements prior to their processing.

Deque Interface

A Deque interface also known as double-queue provides insertion and deletion of elements from both end points.

Map Interface

A Map interface provides key-value pairs, map objects contains keys associated with an value. Maps can not contain duplicate keys and one key can be mapped to atmost one element. In Java Map interface provides three general purpose implementations.

1) HashMap
2) TreeMap
3) LinkedHashMap

1) HashMap

A HashMap is a HashTable implementation of Map interface, unlike HashTable it contains null keys and values. HashMap does not guarantees that the order of the objects will remain same over the time.
package com.beingjavaguys.core;
import java.util.HashMap;

public class HashMapImplementation {

    public static void main(String args[]){

    HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

    hashMap.put(0,"value1");
    hashMap.put(1,"value2");
    hashMap.put(2,"value3");
    System.out.println(hashMap);

    hashMap.remove(0);
    System.out.println(hashMap);
    }
}


2) TreeMap

A TreeMap provides red-black tree based implementation of Map interface.
package com.beingjavaguys.core;
import java.util.TreeMap;

public class TreeMapImplementation {

    public static void main(String args[]){

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

        treeMap.put("key1",123);
        treeMap.put("key2",435);
        treeMap.put("key3",654);
        System.out.println(treeMap);

        treeMap.remove("key1");
        System.out.println(treeMap);
    }
}


3) LinkedHashMap

It is HashTable and LinkedList implementation of Map interface. LinkedHashMap has a double-linkedList running through all its elements.
package com.beingjavaguys.core;
import java.util.HashMap;

public class LinkedHashMapImplementation {

    public static void main(String args[]){

        HashMap<Integer, String> linkedHashMap = new HashMap<Integer, String>();
        linkedHashMap.put(0,"value1");
        linkedHashMap.put(1,"value2");
        linkedHashMap.put(2,"value3");

        System.out.println(linkedHashMap);
        linkedHashMap.remove(0);
        System.out.println(linkedHashMap);
        }
}


Here we are done with a brief introduction to Java Collection Framework, in today's discussion we covers List, Set and Map Interfaces and their implementations. In upcoming blogs we will see more about HashSet, ArrayList and HashMap collections and a detailed implementation of these classes and interfaces. br />







Thanks for reading !
Being Java Guys Team




3 comments:

  1. Good Tutorial ah..................

    But in my Idea You should Program to an interface, not to their implementations

    Do you agree with me
    Map linkedHashMap = new HashMap();
    List linkedList = new LinkedList();

    ReplyDelete
  2. Please provide Output.

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft