Hello world!

Welcome to WordPress.com. After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.

Here are some suggestions for your first post.

  1. You can find new ideas for what to blog about by reading the Daily Post.
  2. Add PressThis to your browser. It creates a new blog post for you about any interesting  page you read on the web.
  3. Make some changes to this page, and then hit preview on the right. You can always preview any post or edit it before you share it to the world.

TreeMap Program – Special functions

public static void specialFunctions(String args[])
{
TreeMap<String, Integer> tm1 = new TreeMap<String, Integer>();
tm1.put("banana", 20);
tm1.put("apple", 50);
tm1.put("melon", 40);
tm1.put("guava", 20);
tm1.put("cherry", 30);

System.out.println("\nElements of tm1: " + tm1);
Set mySet = tm1.keySet();
System.out.println("Keys of tm1: " + mySet);

Collection myCol = tm1.values();
System.out.println("Values of tm1: " + myCol);

// USING ITERATOR
System.out.print("\nPrinting keys with Iterator: ");
Iterator it1 = mySet.iterator();
while(it1.hasNext())
{
System.out.print(it1.next() + ", ");
}
// TREEMAP SORT BY VALUE
TreeSet yourSet = new TreeSet();
yourSet.addAll(myCol);
System.out.println("\nTreeMap sort by value: " + yourSet);

// COMPARING TWO TREE MAPS
TreeMap<String, Integer> tm2 = new TreeMap<String, Integer>();
tm2.putAll(tm1);
System.out.println("\nElements of tm2: " + tm2);
if(tm1.equals(tm2))
{
System.out.println("tm1 and tm2 contain same elements\n");
}

System.out.println("\t\tEXTRACTING TREEMAP ELEMENTS\n");

SortedMap m1 = tm1.subMap("banana", "guava");
System.out.println("subMap(\"banana\", \"guava\"): " + m1);

SortedMap m2 = tm1.headMap("guava");
System.out.println("headMap(\"guava\"): " + m2);

SortedMap m3 = tm1.tailMap("guava");
System.out.println("tailMap(\"guava\"): " + m3);
}

TreeMap Progam – General Functions

public static void generalFunctionsDemo(String args[])
{
TreeMap tm1 = new TreeMap();
System.out.println("tm1 isEmpty() before adding elements: " + tm1.isEmpty());
tm1.put("one", 1);
tm1.put("apple", "red");
tm1.put("two", 2);
tm1.put("three", 3);
tm1.put("four", 4);

System.out.println("tm1 isEmpty() after adding elements: " + tm1.isEmpty());
System.out.println("Key/value pairs of tm1:" + tm1);

System.out.println("\napple key exists: " + tm1.containsKey("apple"));
System.out.println("two key exists: " + tm1.containsKey("two"));
System.out.println("red value exists: " + tm1.containsValue("red"));
System.out.println("2 value exists: " + tm1.containsValue(2));

System.out.println("\nValue of three: " + tm1.get("three"));
System.out.println("Value of four: " + tm1.get("four"));

System.out.println("\nNo. of elements before removal: " + tm1.size());
tm1.remove("one");
System.out.println("No. of elements after removal: " + tm1.size());
tm1.clear();
System.out.println("No. of elements after clear(): " + tm1.size());
}

Null Pointer Exception

Iterate over hashmap

public static void iterateOverHashmapGeneric(){


HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
hm1.put("E", 69);
hm1.put("A", 65);
hm1.put("G", 71);
hm1.put("C", 67);

Set<String> mySet = hm1.keySet();
System.out.print("foreach printing: ");
for(String str : mySet)
{
System.out.print(str + ":" + hm1.get(str) + ", ");
}

HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
hm2.putAll(hm1);
if(hm1.equals(hm2))
{
System.out.println("\n\nhm1 and hm2 contain the same elements");
}

HashMap<String, Integer> hm3 = (HashMap) hm1.clone();
System.out.println("\nElements of hm3: " + hm3);

}


A generics HashMap object hm1 is created that stores keys as strings and values as integers. With put() method elements are added.

Set<String> mySet = hm1.keySet();
for(String str : mySet)
{
System.out.print(str + ":" + hm1.get(str) + ", ");
}

With HashMap, Iterator cannot be used as Iterator stores elements of single entities but HashMap stores pairs. The new JDK 1.5 enhanced for loop (foreach) cannot be used straight away for the same reason. It is used indirectly. The keySet() returns all the keys of HashMap hm1. As keys are single entities, to iterate them, foreach loop is used. The str refers a key and using the key str, the value associated is retrieved with get() method.

HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
hm2.putAll(hm1);
if(hm1.equals(hm2))
{
System.out.println("\n\nhm1 and hm2 contain the same elements");
}

Another generics object hm2 is created and all the elements of hm1 are added at a time with putAll() method. equals() method is used to check both hm1 and hm2 contains the same elements are not. As they have same, the method returns true.

HashMap<String, Integer> hm3 = (HashMap) hm1.clone();
System.out.println("\nElements of hm3: " + hm3);

Any Collection object including HashMap can be cloned. As the cloned object, hm3 and the hm1 occupy different locations and have their own set of values, they maintain perfect encapsulation.

Iterator vs ListIterator

ConcurrentModificationException