Thursday, October 30, 2014

Java Hashmap Pro



Simple Java Linked Hash Map

import java.util.LinkedHashMap;
public class JavaLinkedHashMapExample
{
public static void main(String[] args)
{
LinkedHashMap lHashMap = new LinkedHashMap();
lHashMap.put("One", new Integer(1));
lHashMap.put("Two", new Integer(2));
Object obj = lHashMap.get("One");
System.out.println(obj);  
    }
}

Check if a particular element exists in Java Linked Hash Set


import java.util.LinkedHashSet;
public class CheckElementLinkedHashSetExample
{
public static void main(String[] args)
{
LinkedHashSet lhashSet = new LinkedHashSet();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
boolean blnExists = lhashSet.contains(new Integer("3"));
System.out.println("3 exists in LinkedHashSet ? : " + blnExists);
   }
}

Copy all elements of Java Linked Hash Set to an Object Array

import java.util.LinkedHashSet;  
public class CopyElementsOfHashSetToArrayExample
{
public static void main(String[] args)
{
LinkedHashSet lhashSet = new LinkedHashSet();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
Object[] objArray = lhashSet.toArray();
System.out.println("LinkedHashSet elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
    }
}

Get Size of Java Linked Hash Set

import java.util.LinkedHashSet;  
public class GetSizeOfJavaLinkedHashSetExample
{
public static void main(String[] args)
{
LinkedHashSet lhashSet = new LinkedHashSet();System.out.println("Size of LinkedHashSet : " + lhashSet.size());
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println("Size of LinkedHashSet after addition : " + lhashSet.size());
lhashSet.remove(new Integer("1"));
System.out.println("Size of LinkedHashSet after removal : " + lhashSet.size());
    }
}

Iterate through elements of Java Linked Hash Set

import java.util.LinkedHashSet;
import java.util.Iterator;
public class IterateThroughElementsOfLinkedHashSetExample
{
public static void main(String[] args)
{
LinkedHashSet lhashSet = new LinkedHashSet();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
Iterator itr = lhashSet.iterator();

System.out.println("LinkedHashSet contains : ");
while(itr.hasNext())
System.out.println(itr.next());
   }
}

Remove all elements from Java Linked Hash Set

import java.util.LinkedHashSet;
public class RemoveAllElementsFromLinkedHashSetExample
{
public static void main(String[] args)
{
LinkedHashSet lhashSet = new LinkedHashSet();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));   
System.out.println("LinkedHashSet before removal : " + lhashSet);
lhashSet.clear();
System.out.println("LinkedHashSet after removal : " + lhashSet);
System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty());    
    }
}

Remove specified element from Java Linked Hash Set

import java.util.LinkedHashSet;
public class RemoveSpecifiedElementFromLinkedHashSetExample
{  
public static void main(String[] args)
{
LinkedHashSet lhashSet = new LinkedHashSet();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println("LinkedHashSet before removal : " + lhashSet);
boolean blnRemoved = lhashSet.remove(new Integer("2"));
System.out.println("Was 2 removed from LinkedHashSet ? " + blnRemoved);
System.out.println("LinkedHashSet after removal : " + lhashSet);
   }
}

No comments:

Post a Comment