In this blog we will be learning all the operations we can do in Linked Hash Map , Linked Hash Set and Vector , this blog will be very useful for those want to learn Java Collections
Linked Hash Map
The LinkedHashMap class of the Java collections framework provides the hash table and linked list implementation of the Map interface. The LinkedHashMap interface extends the HashMap class to store its entries in a hash table. It internally maintains a doubly-linked list among all of its entries to order its entries.
package collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class LinkedHashMapExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashMap<String,String> matches=new LinkedHashMap<String,String>();
matches.put("banana", "mango");
matches.put("onion", "tomato");
matches.put("garlic"," ginger");
System.out.println(matches); // insertion order maintained
// if u add null key
matches.put(null,"salt"); // null key is added , if 1 null key is added
//System.out.println(matches);
matches.put(null,"pepper"); // if u add 2nd null key again , value is overwritten
//System.out.println(matches);
matches.put(null,null);
System.out.println(matches);
HashMap<String,String> hasMapMatches=new HashMap<String,String>();
hasMapMatches.put("banana", "mango");
hasMapMatches.put("onion", "tomato");
hasMapMatches.put("garlic"," ginger");
System.out.println(hasMapMatches); // insertion order NOT maintained, random order
hasMapMatches.put(null,"salt");
//System.out.println(hasMapMatches);
hasMapMatches.put(null,"pepper");
System.out.println(hasMapMatches); // if u add second null , value is overwritten
}
// try the same with hash set
}
Linked Hash Set
LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set. That is, when cycling through a LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.
package collections;
import java.util.LinkedHashSet;
public class LinkedHashSetExample {
/**
* LinkedHashSet->Child class of HashSet
* DS-> Hash table + Linked List
* Insertion order is preserved
* Duplicates not allowed
*/
public void linkedHashSet_Example(){
LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add(1);
linkedHashSet.add("A");
linkedHashSet.add("B");
linkedHashSet.add("C");
linkedHashSet.add("10");
System.out.println("Insertion Order preserved Linked Hash Set :"+ linkedHashSet);
System.out.println(" size: "+linkedHashSet.size() );
}
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashSetExample example= new LinkedHashSetExample();
example.linkedHashSet_Example();
}
}
Vector
The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
package collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
public class VectorExample {
/**
* Underlying DS= Resizable Array or Growable Array
* Duplicates Allowed
* Insertion order is preserved
* NULL insertion is possible
* Heterogeneous objects are allowed
* Implements- Serializable, Cloneable and Random Access Interfaces
* Most of the methods present in Vector is Synchronized and it is Thread safe.
*
*/
/**
* Vector is present since Java 1.0 and is known as Legacy Class.
* The operations are much like ArrayList (introduced in 1.2) but the method names are bit lengthy.
* Vector is threadsafe and synchronized and gives poor perfromance.
*/
public void vectorExample(){
//Ways to create a Vectr Object
Vector<String> vector= new Vector<String>();
//Vector vector = new Vector(3);Vector object= new Vector(int initialCapacity)
//Vector vector= new Vector(4, 6); Vector object= new vector(int initialcapacity, capacityIncrement)
//Adding elements -> addElement()
vector.addElement("Steve");
vector.addElement("Bill");
vector.addElement(null);
vector.addElement("Sundar");
vector.addElement("Satya");
vector.addElement("Sergey");
System.out.println("Vector "+ vector);
//Retrieve element -> elementAt()
System.out.println("Element At 0th pos is "+ vector.elementAt(0));
// remove element ->removeElement()
vector.removeElement(null);
System.out.println(vector);
//sublist
System.out.println(vector.subList(1, 3));
//sorting the elements
Collections.sort(vector);
System.out.println("After sorting :"+ vector);
//copy elements from Vector to vector
Vector<String> vector2= new Vector<String>();
vector2.addElement("A");
vector2.addElement("B");
vector2.addElement("C");
System.out.println("Vector1 before copying:" + vector);
System.out.println("Vector2 before copying:" + vector2);
//Copying all the vector to vector2
Collections.copy(vector, vector2);
System.out.println("Vector1 after copying:" + vector);
System.out.println("Vector2 after copying:" + vector2);
//Size and capacity
Vector<String> vector3= new Vector<String>(5);
vector3.addElement("A");
vector3.addElement("B");
System.out.println("Vector size is :"+ vector3.size());
System.out.println("Vector capacity is :"+ vector3.capacity());
}
/*
* Iterate using Enumeration
*/
public void usingEnumeration(){
System.out.println("Using Enumeration");
System.out.println("=================");
Vector<String> vector= new Vector<String>();
vector.addElement("A");
vector.addElement("B");
vector.addElement("C");
Enumeration<String> enumerator= vector.elements();
while(enumerator.hasMoreElements()){
System.out.println(enumerator.nextElement());
}
System.out.println("=================");
}
/*
* Using Iterator
*/
public void usingIterator(){
System.out.println("Using Iterator");
System.out.println("=================");
Vector<String> vector= new Vector<String>();
vector.addElement("A");
vector.addElement("B");
vector.addElement("C");
Iterator< String> iterator= vector.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("=================");
}
/*
* Using ListIterator (We can traverse in forward and reverse direction)
*/
public void usingListIterator(){
System.out.println("Using ListIterator");
System.out.println("=================");
Vector<String> vector= new Vector<String>();
vector.addElement("A");
vector.addElement("B");
vector.addElement("C");
ListIterator< String> iterator= vector.listIterator();
System.out.println("Forward Traversing");
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("=================");
System.out.println("Reverse Traversing");
while(iterator.hasPrevious()){
System.out.println(iterator.previous());
}
System.out.println("=================");
}
//Convert Vector to List
public void vectorToList(){
Vector<String> vector= new Vector<String>();
vector.addElement("A");
vector.addElement("B");
vector.addElement("C");
System.out.println("Vector Elements :"+vector);
List<String> list=Collections.list(vector.elements());
System.out.println("List elements copied from Vector "+ list);
}
//Convert Vector to ArrayList
public void vectorToArrayList(){
Vector<String> vector= new Vector<String>();
vector.addElement("A");
vector.addElement("B");
vector.addElement("C");
System.out.println("Vector Elements :"+vector);
ArrayList<String> arrayList= new ArrayList<String>(vector); //conversion is happening here
System.out.println("ArrayList elements "+ arrayList);
}
public static void main(String[] args) {
VectorExample example = new VectorExample();
example.vectorExample();
//example.usingEnumeration();
//example.usingIterator();
//example.usingListIterator();
//example.vectorToList();
//example.vectorToArrayList();
}
}
Conclusion:
I hope, This article will help you to understand Hash Map and Hash Set , how to add , remove , insert , update , delete, iterate ....
Please Refer Part 1 for Array List and Linked List
Please Refer Part 2 for Map and Set.
You must have got an idea on the topics explained in this blog. Lets explore more and learn New Topics.
Happy Learning
Commenti