Tuesday, 3 June 2014

HashMap : clone() : Returns a shallow copy of this HashMap instance

Object clone()
Returns a shallow copy of this HashMap instance.

import java.util.*;

class HashMapClone{
  public static void main(String args[]){
    HashMap<Integer, String> myMap1 = new HashMap<> ();
  HashMap<Integer, String> myMap2 = new HashMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, "First");
  myMap1.put(2, "Second");
  myMap1.put(3, "Third");
  myMap1.put(4, "Fourth");
  
  System.out.println("Data in myMap1 is");
  System.out.println(myMap1);
  
  System.out.println("\nCloning myMap1 to myMap2");
  myMap2 = (HashMap)myMap1.clone();
  System.out.println("\nData in myMap2 is");
  System.out.println(myMap2);
  }
}

Output
Data in myMap1 is
{1=First, 2=Second, 3=Third, 4=Fourth}

Cloning myMap1 to myMap2

Data in myMap2 is
{1=First, 2=Second, 3=Third, 4=Fourth}



Prevoius                                                 Next                                                 Home

HashMap : clear() : Removes all the mappings from this map

void clear()
Removes all of the mappings from this map.

import java.util.*;

class HashMapClear{
  public static void main(String args[]){
  Map<Integer, String> myMap1 = new HashMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, "First");
  myMap1.put(2, "Second");
  myMap1.put(3, "Third");
  myMap1.put(4, "Fourth");
  
  System.out.println("Data in myMap1 is");
  System.out.println(myMap1);
  
  System.out.println("\nCalling clear method on map");
  myMap1.clear();
  
  System.out.println("\nData in myMap1 is");
  System.out.println(myMap1);
  }
}


Output
Data in myMap1 is
{1=First, 2=Second, 3=Third, 4=Fourth}

Calling clear method on map

Data in myMap1 is
{}

1. Implement the clear functionality for the HashMap
import java.util.*;

class HashMapClearEx{

  static void clearMap(Map myMap1){
    Set<Integer> mySet = myMap1.keySet();
  
  int size = myMap1.size();
  Object keys[] = new Object[size];
  int i=0;
  
  Iterator iter1 = mySet.iterator();
  
  while(iter1.hasNext()){
    keys[i] = iter1.next();
  i++;
  }
  
  for(i=0; i < size; i++)
  myMap1.remove(keys[i]);
  }
  
  public static void main(String args[]){
  Map<Integer, String> myMap1 = new HashMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, "First");
  myMap1.put(2, "Second");
  myMap1.put(3, "Third");
  myMap1.put(4, "Fourth");
  
  System.out.println("Data in myMap1 is");
  System.out.println(myMap1);
  
  System.out.println("\nCalling clear method on map");
  clearMap(myMap1);
  
  System.out.println("\nData in myMap1 is");
  System.out.println(myMap1);
  }
}

Output
Data in myMap1 is
{1=First, 2=Second, 3=Third, 4=Fourth}

Calling clear method on map

Data in myMap1 is
{}

Method “clearMap” gets all the keys of the given Map using iterator and removes each key, value mapping from the Map.

Prevoius                                                 Next                                                 Home

HashMap(Map m)

HashMap(Map<? extends K,? extends V> m)
Constructs a new HashMap with the same mappings as the specified Map. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.

import java.util.*;

class HashMapConstructor4{
  public static void main(String args[]){
  Map<Integer, Float> myMap1 = new HashMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, 0.1f);
  myMap1.put(2, 0.2f);
  myMap1.put(3, 0.3f);
  myMap1.put(4, 0.4f);
  
  Map<Integer, Float> myMap2 = new HashMap<> (myMap1);
  
  System.out.println("Data in myMap2 is");
  System.out.println(myMap2);
  }
}

Output
Data in myMap2 is
{1=0.1, 2=0.2, 3=0.3, 4=0.4}

1. throws NullPointerException if the specified map is null
import java.util.*;

class HashMapConstructor4NullPointer{
  public static void main(String args[]){
  Map<Integer, Float> myMap1 = null; 
  Map<Integer, Float> myMap2 = new HashMap<> (myMap1);
  }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.HashMap.<init>(HashMap.java:318)
        at HashMapConstructor4NullPointer.main(HashMapConstructor4NullPointer.ja
va:6)



Prevoius                                                 Next                                                 Home

HashMap (int initialCapacity, float loadFactor)

HashMap(int initialCapacity, float loadFactor)
Constructs an empty HashMap with the specified initial capacity and load factor.

import java.util.*;

class HashMapConstructor3{
  public static void main(String args[]){
    Map<Integer, String> myMap = new HashMap<> (10, 0.9f);
  
  /* Adding Data to myMap */
  myMap.put(1, "First");
  myMap.put(2, "Second");
  myMap.put(3, "Third");
  myMap.put(4, "Fourth");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  }
}

Output
Elements in myMap are
{1=First, 2=Second, 3=Third, 4=Fourth}

1. throws IllegalArgumentException if the initial capacity is negative
or the load factor is nonpositive
import java.util.*;

class HashMapConstructor3Illegal{
  public static void main(String args[]){
    Map<Integer, String> myMap = new HashMap<> (10, 0);
  }
}

Output
Exception in thread "main" java.lang.IllegalArgumentException: Illegal load fact
or: 0.0
        at java.util.HashMap.<init>(HashMap.java:273)
        at HashMapConstructor3Illegal.main(HashMapConstructor3Illegal.java:5)


Prevoius                                                 Next                                                 Home

HashMap (int initialCapacity)

HashMap(int initialCapacity)
Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).

import java.util.*;

class HashMapConstructor2{
  public static void main(String args[]){
    Map<Integer, String> myMap = new HashMap<> (10);
  
  /* Adding Data to myMap */
  myMap.put(1, "First");
  myMap.put(2, "Second");
  myMap.put(3, "Third");
  myMap.put(4, "Fourth");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  }
}

Output
Elements in myMap are
{1=First, 2=Second, 3=Third, 4=Fourth}

1. Throws IllegalArgumentException if the initial capacity is negative
import java.util.*;

class HashMapConstructor2Illegal{
  public static void main(String args[]){
    Map<Integer, String> myMap = new HashMap<> (-10);
  }
}

Output
Exception in thread "main" java.lang.IllegalArgumentException: Illegal initial c
apacity: -10
        at java.util.HashMap.<init>(HashMap.java:268)
        at java.util.HashMap.<init>(HashMap.java:297)
        at HashMapConstructor2Illegal.main(HashMapConstructor2Illegal.java:5)



Prevoius                                                 Next                                                 Home

HashMap()

HashMap()
Constructs an empty HashMap with the default initial capacity 16 and the default load factor 0.75. 

Example
import java.util.*;

class HashMapConstructor1{
  public static void main(String args[]){
    Map<Integer, String> myMap = new HashMap<> ();
  
  /* Adding Data to myMap */
  myMap.put(1, "First");
  myMap.put(2, "Second");
  myMap.put(3, "Third");
  myMap.put(4, "Fourth");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  }
}

Output
Elements in myMap are
{1=First, 2=Second, 3=Third, 4=Fourth}





Prevoius                                                 Next                                                 Home

Constructors Of HashMap

HashMap provides Below constructors
HashMap()
HashMap(int initialCapacity)
HashMap(int initialCapacity, float loadFactor)
HashMap(Map<? extends K,? extends V> m)


Prevoius                                                 Next                                                 Home