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]
- Storing Java Object In ArrayList
- How To Copy ArrayList To Array
- How to Convert Array to ArrayList
- How To Shallow Copy Or Clone a ArrayList
- Reverse order of all elements of ArrayList
- How to add all elements of a list to ArrayList
- Swap elements of Java ArrayList example
- Remove All Element From Arraylist Example
- how To Remove Element of arraylist
- How to Remove List From Arraylist
- How to get sub list from ArrayList
- How to get element of Arraylist
- How to find does ArrayList conains elements or not
- How to shuffle elements in ArrayList
- How to count number of element in ArrayList
- How to replace element in ArrayList
- How to insert element in arraylist
- How to use retailAll in arraylist
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]
- Linkedlist Examples :
- How to create LinkedList in Java
- How iterate LinkedList by using iterator
- How Convert Array to LinkedList
- How To make Shallow Copy Or Clone a LinkedList
- How to reverse elements of LikedList
- How to add all elements of a list to LinkedList
- How Swap elements of Java LinkedList
- How Remove All Element From LinkedList Example
- How to Remove List From LinkedList
- How to get sub list from LinkedList
- How to get element of LinkedList
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 :
- How to create Vector In java
- How to storing Java Object In Vector
- Vector Iterator Example
- How To Copy Vector To Array
- How to Convert Array to Vector
- How To Shallow Copy Or Clone a Vector
- Reverse order of all elements of Vector
- How to add all elements of a list to Vector
- How to swap elements of vector
- How to remove all element from vector
- How To Remove Element of Vector
- How to Remove List From Vector
- How to get sub list from Vector
- How to get element of vector
- How to find does Vector contains elements or not
- How to shuffle elements in Vector
- How to count number of element in vector
- How to replace element of vector
- How to insert element in vector
- How to use retailAll in vector
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:
- How to create Hashset In Java
- How to storing Java Object In HashSet
- How to Convert a HashSet to an ArrayList
- How to convert array to HashSet
- How to copy all element of a Hashset to other
- How To Remove Element of Hashset
- How to remove all element from HashSet
- How to Remove Set From HashSet
- How To Convert Hashset To Array
- How to find does HashSet contains elements or not
- How to count number of element in HashSet
- How to check HashSet Empty Or not
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:
- How to Create LinkedHashSet In Java
- How to storing Java Object In LinkedHashSet
- How to Convert a LinkedHashSet to ArrayList
- How to convert array to LinkedHashSet
- How to copy all element of a LinkedHashset to other
- How To Remove Element of LinkedHashset
- How to remove all element from LinkedHashSet
- How to Remove Set From LinkedHashSet
- How To Convert LinkedHashset To Array
- How to find does LinkedHashSet contains elements or not
- How to count number of element in LinkedHashSet
- How to check LinkedHashSet Empty Or not
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]
TreeSet Examples:
- How to create TreeSet In Java
- How to Convert a TreeSet to ArrayList
- How to convert array to TreeSet
- How to sort TreeSet with user defined objects.
- How to copy all element of a Treeset to other
- How To Remove Element of Treeset
- How to remove all element from TreeSet
- How to Remove Set From TreeSet
- How To Convert Treeset To Array
- How to find does TreeSet contains elements or not
- How to count number of element in TreeSet
- How to check TreeSet Empty Or not
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)
}
}
HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}
HashMap Examples:
- How to create HashMap In Java
- How to iterate through HashMap
- How to storing Java Object In HashMap
- How to Search key in HashMap Java
- How to Search Value in HashMap Java
- How to get value of key in HashMap
- How to get all keys of HashMap
- How to get size of HashMap
- How to get entry set from HashMap
- How to delete all elements from HashMap
- How to Print Hashmap In Java
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:
- How to create TreeMap In Java
- How to iterate through TreeMap
- How to storing Java Object In TreeMap
- How to Search key in TreeMap Java
- How to Search Value in TreeMap Java
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:
- How to create HashTable In Java
- How to iterate through Hashtabl
- How to storing Java Object In HashTable
- How to Search key in Hashtable Java
- How to Search Value in Hashtable Java
- How to get value of key in HashTable
- How to get all keys of HashTable
- How to get size of hashtable
- How to get entry set from Hashtable
- How to delete all elements from Hashtable
- How to get elements using Enumeration from Hashtable
- How to create shallow copy of Hashtable