Collections In Java

Java collections is a collection of individual objects that are represented as a single unit or group.
Java Collections framework contains the interfaces and classes which helps in working with different types of collections such as lists, sets, maps, stacks and queues etc.Java collection framework have ready to use different important functionality to perform diff type operations like that insertion , updation , deletion, searching, sorting etc.
These functionalities make a developer work very easy and fast.In this post we will discuss different interface and classes used in java collection framework.

What is collection in java?

Java collection framework represents a unified architecture to store and manipulate a group of objects.
A Java collection framework includes the following:

Java Collections Hierarchy

Collection :
Collection interface is root interface in collection framework. This interface have general purpose
method like add , remove , all all which is implemented by other interface like list , set (except to map).

List : List is ordered collection . List can have duplicate element. It can be access element by indexes .
Index start from zero.It have control over insertion position of
element.It provides some method where we can pass position of element in list.
List interface is implemented by the class ArrayList, CopyOnWriteArrayList, LinkedList, Stack and Vector

ArrayList: ArrayList represents re sizable array. ArrayList maintain insertion order of element and it is non synchronized.We can access element of arraylist by index. There is basic example how to create arraylist in java.

import java.util.ArrayList;

public class CreateArrayList {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
// add element in arraylist
// print arraylist
System.out.println(“arrayList is = ” + arr);



arrayList is = [c, php, html, java]
Arraylist Examples:

LinkedList : LinkedList is doubly linked list implementation of list interface. It maintain insertion order and non synchronized.It is fast for insertion or deletion in middle of list because it used doubly linked list to store data. Now we will learn how to create linkedlist in java.

import java.util.LinkedList;

public class CreateLinkList {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
System.out.println(“Linkedlist is”+linkedList);


Linkedlist is = [Mumbai, Delhi, Noida, Gao, Patna]

Vector : Vector implemented list interface. Vector maintain insertion order of element and it is used dynamic array to store element like arraylist. it is synchronized , so it is slow performance wise for operations. Now we will vector in java .

import java.util.Vector;
public class CreateVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
// print vector
System.out.println(“Vector = “+vector);


Vector = [cricket, hockey, football, tennish]

Vector Examples :

Set interface : It is implementation of collection interface and Set does not allow duplicate values. Set interface does not provides no guarantee to return the elements in any predictable order. Set interface implemented by these classes ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, LinkedHashSet and TreeSet.

HashSet :It is implementation of set interface.It is use hashtable to store element.It is maintain unique element . it is not give any guarantee iterating order and it is allow one null value.Now we learn how to create hashSet in java.

import java.util.HashSet;
import java.util.Set;
public class CreateHashset {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
//print hashset
System.out.println(“hashset = “+hs);


hashset = [javascript, php, html, java, vb]

HashSet Examples:

LinkedHashSet : It is extends hashset class and implement set interface. it is not allowed duplicate element. it is maintain insertion order and allow one null value. Now we will create LinkedHashSet in Java.

import java.util.LinkedHashSet;
import java.util.Set;
public class CreateLinkedHashSet {
public static void main(String[] args) {
Set<String> lhs = new LinkedHashSet<String>();
//print linkedhashset
System.out.println(“lhs is = “+lhs);


lhs is = [mumbai, delhi, kolkata, chandigarh, dehradun]

LinkedHashSet Examples:

TreeSet : TreeSet implement the set interface. It is use tree for storage. it is not allowed duplicate element. It is maintain ascending order of element.Now we will create TreeSet in Java.

package com.javapro.treeset;
import java.util.Set;
import java.util.TreeSet;
public class CreateTreeSet {
public static void main(String[] args) {
    Set<String> ts = new TreeSet<String>();
    // print treeset
    System.out.println(” ts =”+ts);


ts =[chandigarh, dehradun, delhi, kolkata, mumbai]
Map Interfaces : Map interface store data in key and value pairs . Key must be unique and each key must have values. Map interface implement by the classes ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, Properties, TreeMap and WeakHashMap.
HashMap : Hashmap implement map interface. Hashmap store data in key values pair and each key mapped to value. Now we will learn how to create hashmap in java.
import java.util.HashMap;
import java.util.Map;
public class CreateHashMap {
   public static void main(String[] args) {
         Map<Integer,String> map = new HashMap<Integer,String>();
          Associates the specified value with the specified key in
          this map (optional operation). If the map previously
          contained a mapping for the key, the old value is
          replaced by the specified value
       map.put(2, “amit”);
       map.put(5, “yogesh”);
       //print hashmap
      System.out.println(“HashMap = “+map)
HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

HashMap Examples:

TreeMap : TreeMap store data in key values pair like hashmap. Differences is that treemap sort the key       in natural order. Now we will learn how to create TreeMap in java.

import java.util.Map;
import java.util.TreeMap;
public class CreateTreeMap {
    public static void main(String[] args) {
         Map<Integer, String> tm = new TreeMap<Integer,String>();
         // add key and values
         tm.put(5, “mysql”);
         //print treemap
         System.out.println(“TreeMap Is=”+tm);


TreeMap Is={1=java, 2=cpp, 3=php, 4=c, 5=mysql}

TreeMap Examples:

Hashtable: hashtable just like hashmap different is that hashtable is synchronized and hashmap is not.Now we will create Hashtable in java.

import java.util.Hashtable;

public class CreateHashTable {
public static void main(String[] args) {
   Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
     * Maps the specified key to the specified
     * value in this hashtable
  ht.put(1, “java”);
  ht.put(2, “jersey”);
  ht.put(3, “spring”);
  ht.put(4, “c”);
  ht.put(5, “php”);
  //print hashtable
 System.out.println(“Hash Table is =”+ht);


Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

HashTable Examples:

References :

Java ArrayList Examples

Java ArrayList Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 39 = 43