Thursday, 5 June 2014

Constructors Of TreeMap

TreeMap provides Below constructors.
TreeMap()
TreeMap(Comparator<? super K> comparator)
TreeMap(Map<? extends K,? extends V> m)
TreeMap(SortedMap<K,? extends V> m)

Prevoius                                                 Next                                                 Home

TreeMap

TreeMap is a member of the Java Collections Framework. TreeMap is a reb black tree based implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

TreeMap performs operations like containsKey, get, put and remove in O(log n) time.

TreeMap is not synchronized. So in Multi threaded Environment, it must be synchronized externally.

Prevoius                                                 Next                                                 Home

Wednesday, 4 June 2014

Difference between hashSet and hashMap

  1. HashSet is an implementation of Set Interface. HashMap is a implementation of Map interface
  2. HashSet stores Objects. HashMap stores data in <Key,Value> pair format.
  3. HashSet uses add method to add elements to HashSet, where as HashMap uses put method, which takes two arguments key, value.
  4. HashSet internally uses HashMap to store data.
  5. In case of the HashSet, adding the same object do nothing. In case of a HashMap, putting a new key,value pair with an existing key will overwrite the existing value to set a new value for that key.
  6. HashSet contains unique Keys. HashMap contains unique keys and allow duplicate values.

Prevoius                                                 Next                                                 Home

HashMap : values : Get All the Values in the Map

public Collection<V> values()
Returns a Collection view of the values contained in this map.

import java.util.*;

class HashMapValues{
 public static void main(String args[]){
  HashMap<Integer, String> myMap;
  Collection<String> myCollection;
  
  myMap = new HashMap<> ();
  
  /* Add Data to myMap */
  myMap.put(100, "Hi");
  myMap.put(25, "How");
  myMap.put(31, "Are");

  myCollection = myMap.values();
  
  System.out.println("Values in myMap are");
  System.out.println(myCollection);  
 }
}

Output
Values in myMap are
[Hi, How, Are]

1. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
import java.util.*;

class HashMapValues1{
 public static void main(String args[]){
  HashMap<Integer, String> myMap;
  Collection<String> myCollection;
  
  myMap = new HashMap<> ();
  
  /* Add Data to myMap */
  myMap.put(100, "Hi");
  myMap.put(25, "How");
  myMap.put(31, "Are");

  myCollection = myMap.values();
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  System.out.println("Values in myMap are");
  System.out.println(myCollection);

  System.out.println("\nRemoving the value \"How\" from myCollection");
  myCollection.remove("How");
  
  System.out.println("\nElements in myMap are");
  System.out.println(myMap);
  System.out.println("Values in myMap are");
  System.out.println(myCollection);
  
  System.out.println("\nAdding Key 99 to myMap");
  myMap.put(99, "wxyz");
  
  System.out.println("\nElements in myMap are");
  System.out.println(myMap);
  System.out.println("Values in myMap are");
  System.out.println(myCollection);  
 }
}

Output
Elements in myMap are
{100=Hi, 25=How, 31=Are}
Values in myMap are
[Hi, How, Are]

Removing the value "How" from myCollection

Elements in myMap are
{100=Hi, 31=Are}
Values in myMap are
[Hi, Are]

Adding Key 99 to myMap

Elements in myMap are
{99=wxyz, 100=Hi, 31=Are}
Values in myMap are
[wxyz, Hi, Are]



Prevoius                                                 Next                                                 Home

HashMap : size : Get the Number Of Key-Value Pairs

public int size()
Returns the number of key-value mappings in this map.

import java.util.*;

class HashMapSize{
 public static void main(String args[]){
  HashMap<Integer, String> myMap;
  myMap = new HashMap<> ();
  
  System.out.print("Size Of myMap is ");
  System.out.println(myMap.size());
  
  /* Add Data to myMap */
  myMap.put(100, "Hi");
  myMap.put(25, "How");
  myMap.put(31, "Are");
 
  System.out.print("Size Of myMap is ");
  System.out.println(myMap.size());
 }
}


Output
Size Of myMap is 0
Size Of myMap is 3




Prevoius                                                 Next                                                 Home

HashMap : remove : remove Mapping

public V remove(Object key)
Removes the mapping for the specified key from this map if present. Return the previous value associated with key, or null if there was no mapping for key.

import java.util.*;

class HashMapRemove{
 public static void main(String args[]){
  HashMap<Integer, String> myMap;
  myMap = new HashMap<> ();
  
  /* Add Data to myMap */
  myMap.put(100, "Hi");
  myMap.put(25, "How");
  myMap.put(31, "Are");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.print("Removing the Mapping for the Key 25 ");
  System.out.println(myMap.remove(25));
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
 }
}


Output
Elements in myMap are
{100=Hi, 25=How, 31=Are}
Removing the Mapping for the Key 25 How
Elements in myMap are
{100=Hi, 31=Are}


Prevoius                                                 Next                                                 Home

HashMap : putAll : Copy All Mapping's to Specific Map

public void putAll(Map<? extends K, ? extends V> m)
Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

import java.util.*;

class HashMapPutAll{
 public static void main(String args[]){
  HashMap<Integer, String> myMap1;
  Map<Integer, String> myMap2;
  
  myMap1 = new HashMap<> ();
  myMap2 = new TreeMap<> ();
  
  /* Add Data to myMap */
  myMap2.put(100, "Hi");
  myMap2.put(25, "How");
  myMap2.put(31, "Are");
  
  System.out.println("Elements in myMap2 are");
  System.out.println(myMap2);
  
  myMap1.putAll(myMap2);
  
  System.out.println("Elements in myMap1 are");
  System.out.println(myMap1);
 }
}


Output
Elements in myMap2 are
{25=How, 31=Are, 100=Hi}
Elements in myMap1 are
{25=How, 100=Hi, 31=Are}

1. Throws NullPointerException if the specified map is nul
import java.util.*;

class HashMapPutAllNullPointer{
 public static void main(String args[]){
  HashMap<Integer, String> myMap1;
  Map<Integer, String> myMap2;
  
  myMap1 = new HashMap<> ();
  myMap2 = null;
  
  System.out.println("Elements in myMap2 are");
  System.out.println(myMap2);
  
  myMap1.putAll(myMap2);
  
  System.out.println("Elements in myMap1 are");
  System.out.println(myMap1);
 }
}

Output
Elements in myMap2 are
null
Exception in thread "main" java.lang.NullPointerException
        at java.util.HashMap.putMapEntries(HashMap.java:500)
        at java.util.HashMap.putAll(HashMap.java:784)
        at HashMapPutAllNullPointer.main(HashMapPutAllNullPointer.java:14)


Prevoius                                                 Next                                                 Home