Thursday, October 30, 2014

JAVA FANTASTIC PROGRAM



Check if a particular key exists in Java Tree Map

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

Check if a particular value exists in Java Tree Map

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

Get Head Map from Java Tree Map

import java.util.SortedMap;
import java.util.TreeMap;
public class GetHeadMapFromTreeMapExample
{
 public static void main(String[] args)
{
TreeMap treeMap = new TreeMap();
treeMap.put("1","One");
treeMap.put("3","Three");
treeMap.put("2","Two");
treeMap.put("5","Five");
treeMap.put("4","Four");
SortedMap sortedMap = treeMap.headMap("3");
System.out.println("Head Map Contains : " + sortedMap);
   }
}

Get Set view of Keys from Java Tree Map

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

Get Size of Java Tree Map

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

Get Sub Map from Java Tree Map

import java.util.TreeMap;
import java.util.SortedMap;
public class GetSubMapFromTreeMapExample
{
public static void main(String[] args)
{
TreeMap treeMap = new TreeMap();
treeMap.put("1","One");
treeMap.put("3","Three");
treeMap.put("2","Two");
treeMap.put("5","Five");
treeMap.put("4","Four");
SortedMap sortedMap = treeMap.subMap("2","5");
System.out.println("SortedMap Contains : " + sortedMap);
   }
}

Get Tail Map from Java Tree Map example


import java.util.SortedMap;
import java.util.TreeMap;
public class GetTailMapFromTreeMapExample
{
public static void main(String[] args)
{
TreeMap treeMap = new TreeMap();
treeMap.put("1","One");
treeMap.put("3","Three");
treeMap.put("2","Two");
treeMap.put("5","Five");
treeMap.put("4","Four");
   }
}

Get lowest and highest key stored in Java Tree Map example

import java.util.TreeMap;
public class GetLowestHighestKeyTreeMapExample
 {
public static void main(String[] args)
{
TreeMap treeMap = new TreeMap();
treeMap.put("1","One");
treeMap.put("3","Three");
treeMap.put("2","Two");
treeMap.put("5","Five");
treeMap.put("4","Four");
   }
}

Iterate through the values of Java Tree Map example

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

Remove all values from Java Tree Map example

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

Remove value from Java Tree Map example

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

No comments:

Post a Comment