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:
Interfaces
Classes
Algorithm

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
arr.add(“c”);
arr.add(“php”);
arr.add(“html”);
arr.add(“java”);
// print arraylist
System.out.println(“arrayList is = ” + arr);
}

}

Output:

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>();
linkedList.add(“Mumbai”);
linkedList.add(“Delhi”);
linkedList.add(“Noida”);
linkedList.add(“Gao”);
linkedList.add(“Patna”);
System.out.println(“Linkedlist is”+linkedList);
}
}

Output:

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
vector.add(“cricket”);
vector.add(“hockey”);
vector.add(“football”);
vector.add(“tennish”);
// print vector
System.out.println(“Vector = “+vector);
}
}

Output:

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>();
hs.add(“java”);
hs.add(“php”);
hs.add(“html”);
hs.add(“javascript”);
hs.add(“vb”);
//print hashset
System.out.println(“hashset = “+hs);
}
}

Output:

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>();
lhs.add(“mumbai”);
lhs.add(“delhi”);
lhs.add(“kolkata”);
lhs.add(“chandigarh”);
lhs.add(“dehradun”);
//print linkedhashset
System.out.println(“lhs is = “+lhs);
}
}

Output:

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>();
    ts.add(“mumbai”);
    ts.add(“delhi”);
    ts.add(“kolkata”);
    ts.add(“chandigarh”);
    ts.add(“dehradun”);
    // print treeset
    System.out.println(” ts =”+ts);
    }
}

Output:

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(1,”ankush”);
       map.put(2, “amit”);
       map.put(3,”shivam”);
       map.put(4,”ankit”);
       map.put(5, “yogesh”);
       //print hashmap
      System.out.println(“HashMap = “+map)
      }
   }
Output:
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(1,”java”);
         tm.put(2,”cpp”);
         tm.put(3,”php”);
         tm.put(4,”c”);
         tm.put(5, “mysql”);
         //print treemap
         System.out.println(“TreeMap Is=”+tm);
      }
  }

Output:

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);
 }
}

Output:

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 *

71 − 63 =