Thursday, October 30, 2014

Java Linked Pro



Check if a particular value exists in Java Linked Hash Map

import java.util.LinkedHashMap;
public class CheckValueOfLinkedHashMapExample
{
public static void main(String[] args)
{
LinkedHashMap lHashMap = new LinkedHashMap();
lHashMap.put("1","One");
lHashMap.put("2","Two");
lHashMap.put("3","Three");
boolean blnExists = lHashMap.containsValue("Two");
System.out.println("Two exists in LinkedHashMap ? : " + blnExists);
   }
}

Get Set view of Keys from Java Linked Hash Map

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
public class GetSetViewOfKeysFromLinkedHashMapExample
{
public static void main(String[] args)
{
LinkedHashMap lHashMap = new LinkedHashMap();
lHashMap.put("1","One");
lHashMap.put("2","Two");
lHashMap.put("3","Three");
Set st = lHashMap.keySet();
System.out.println("Set created from LinkedHashMap Keys contains :");
Iterator itr = st.iterator();
while(itr.hasNext())
System.out.println(itr.next());
st.remove("2");
boolean blnExists = lHashMap.containsKey("2");
System.out.println("Does LinkedHashMap contain 2 ? " + blnExists);
     }
}

Get Size of Java Linked Hash Map

import java.util.LinkedHashMap;  
public class GetSizeOfLinkedHashMapExample
{
public static void main(String[] args)
{
LinkedHashMap lHashMap = new LinkedHashMap();
System.out.println("Size of LinkedHashMap : " + lHashMap.size());
lHashMap.put("1","One");
lHashMap.put("2","Two");
lHashMap.put("3","Three");
System.out.println("Size of LinkedHashMap after addition : " + lHashMap.size());
Object obj = lHashMap.remove("2");
System.out.println("Size of LinkedHashMap after removal : " + lHashMap.size());
   }
}

Iterate through the values of Java Linked Hash Map

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Iterator;
public class IterateValuesOfLinkedHashMapExample
{
public static void main(String[] args)
{
LinkedHashMap lHashMap = new LinkedHashMap();
lHashMap.put("1","One");
lHashMap.put("2","Two");
lHashMap.put("3","Three");
Collection c = lHashMap.values(); Iterator itr = c.iterator();
while(itr.hasNext())
System.out.println(itr.next());
   }
}

Remove all values from Java Linked Hash Map

import java.util.LinkedHashMap;
public class EmptyLinkedHashMapExample
{
public static void main(String[] args)
 {
LinkedHashMap lHashMap = new LinkedHashMap();
lHashMap.put("1","One");
lHashMap.put("2","Two");
lHashMap.put("3","Three");
lHashMap.clear();
System.out.println("Total key value pairs in LinkedHashMap are : "
+ lHashMap.size());
   }
}

Remove value from Java Linked Hash Map

import java.util.LinkedHashMap;
public class RemoveValueFromLinkedHashMapExample
{
public static void main(String[] args)
{
LinkedHashMap lHashMap = new LinkedHashMap();
lHashMap.put("1","One");
lHashMap.put("2","Two");
lHashMap.put("3","Three");
Object obj = lHashMap.remove("2");
System.out.println(obj + " Removed from LinkedHashMap");
    }
}

No comments:

Post a Comment