Wednesday, 4 June 2014

HashMap : put : Put a key, value mapping to the Map

public V put(K key, V value)
Associates the specified value with the specified key in this map.If the map previously contained a mapping for the key, the old value is replaced. Return the previous value associated with key, or null if there was no mapping for key.

import java.util.*;

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


Output
Putting 100 to myMap null
Putting 25 to myMap null
Putting 31 to myMap null
Putting 100 to myMap Hi
Elements in myMap are
{100=new String, 25=How, 31=Are}


Prevoius                                                 Next                                                 Home

HashMap : keySet : Get All the Keys in the Map

public Set<K> keySet()
Returns a Set view of the keys contained in this map.

import java.util.*;

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

  System.out.println("Keys in myMap are");
  System.out.println(mySet);  
 }
}


Output
Keys in myMap are
[100, 25, 31]

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

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

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  System.out.println("Keys in myMap are");
  System.out.println(mySet); 

  System.out.println("\nRemoving Key 100 from mySet");
  mySet.remove(100);
  
  System.out.println("\nElements in myMap are");
  System.out.println(myMap);
  System.out.println("Keys in myMap are");
  System.out.println(mySet); 
  
  System.out.println("\nAdding a Key 99 to myMap");
  myMap.put(99, "wxyz");
  
  System.out.println("\nElements in myMap are");
  System.out.println(myMap);
  System.out.println("Keys in myMap are");
  System.out.println(mySet); 
 }
}

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

Removing Key 100 from mySet

Elements in myMap are
{25=How, 31=Are}
Keys in myMap are
[25, 31]

Adding a Key 99 to myMap

Elements in myMap are
{99=wxyz, 25=How, 31=Are}
Keys in myMap are
[99, 25, 31]

Prevoius                                                 Next                                                 Home

HashMap : isEmpty : Check whether Map is Empty or not

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

import java.util.*;

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


Output
Is myMap Empty true
Is myMap Empty false



Prevoius                                                 Next                                                 Home

HashMap : get(Object key) : Get the Value Associated with the Key

public V get(Object key)
Returns the value to which the specified key is mapped or null if this map contains no mapping for the key.

import java.util.*;

class HashMapGet{
 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("Key 100 Mapped to Value ");
  System.out.println(myMap.get(100));
 }
}

Output
Elements in myMap are
{100=Hi, 25=How, 31=Are}
Key 100 Mapped to Value Hi

Prevoius                                                 Next                                                 Home

Tuesday, 3 June 2014

HashMap : entrySet() : Get all the Entries in the Map

Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.

import java.util.*;

class HashMapEntrySet{
  public static void main(String args[]){
    HashMap<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("Key\tValue");
  Set<Map.Entry<Integer, String>> mySet = myMap1.entrySet();
  
  for(Map.Entry<Integer, String> entry : mySet){
  System.out.print(entry.getKey() +"\t");
  System.out.println(entry.getValue());
  }
  }
}


Output
Key     Value
1       First
2       Second
3       Third
4       Fourth

1. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
import java.util.*;
class HashMapEntrySetEx{
  public static void main(String args[]){
    Map<Integer, String> mapEx = new HashMap<> ();
  
  /* Put the data to Map*/
  mapEx.put(1, "First");
  mapEx.put(2, "Second");
  mapEx.put(3, "Third");
  mapEx.put(4, "Fourth");
  
  System.out.println("Elements in the Map are " + mapEx);
  System.out.println("\nUpdating the Entries using entrySet\n");
  for (Map.Entry<Integer, String> entry : mapEx.entrySet()){
   entry.setValue("default");
  }
  
  System.out.println("Elements in the Map are " + mapEx);
  }
}

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

Updating the Entries using entrySet

Elements in the Map are {1=default, 2=default, 3=default, 4=default}




Prevoius                                                 Next                                                 Home

HashMap : containsValue (Object value) : Search for a value in the Map

boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.

import java.util.*;

class HashMapContainsValue{
  public static void main(String args[]){
    HashMap<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.print("\nIs myMap1 contains Value First ");
  System.out.println(myMap1.containsValue("First"));
  
  System.out.print("Is myMap1 contains Value Last ");
  System.out.println(myMap1.containsValue("Last"));
  }
}

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

Is myMap1 contains Value First true
Is myMap1 contains Value Last false


Prevoius                                                 Next                                                 Home

HashMap : containsKey(Object key) : Search for a key in HashMap

containsKey(Object key)
Returns true if this map contains a mapping for the specified key.

import java.util.*;

class HashMapContainsKey{
  public static void main(String args[]){
    HashMap<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.print("\nIs myMap1 contains key 1 ");
  System.out.println(myMap1.containsKey(1));
  
  System.out.print("Is myMap1 contains key 10 ");
  System.out.println(myMap1.containsKey(10));
  }
}


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

Is myMap1 contains key 1 true
Is myMap1 contains key 10 false

Prevoius                                                 Next                                                 Home