Java - HashMap

java

HashMap<Integer, String> hmap = new HashMap<Integer, String>();
hmap.put(12, "Chaitanya");

Set set = hmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
  Map.Entry mentry = (Map.Entry)iterator.next();
  System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
  System.out.println(mentry.getValue());
}

String var= hmap.get(2);

hmap.remove(3);

Set set2 = hmap.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
  Map.Entry mentry2 = (Map.Entry)iterator2.next();
  System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
  System.out.println(mentry2.getValue());
}

clear()
clone()
containsKey
containsValue
get
isEmpty
keySet
put
size
values()
remove
putAll

How can we use HashMap?

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Details {

    public static void main(String args[]) {

        HashMap<Integer, String> hmap = new HashMap<Integer, String>();

        /*Adding elements to HashMap*/
        hmap.put(12, "Chaitanya");
        hmap.put(2, "Rahul");
        hmap.put(7, "Singh");
        hmap.put(49, "Ajeet");
        hmap.put(3, "Anuj");

        /* Iterate over each item / key */
        Set set = hmap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
            Map.Entry mentry = (Map.Entry)iterator.next();
            System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
            System.out.println(mentry.getValue());
        }

        /* Get values based on key*/
        String var= hmap.get(2);
        System.out.println("Value at index 2 is: "+var);

        /* Remove values based on key*/
        hmap.remove(3);
        System.out.println("Map key and values after removal:");
        Set set2 = hmap.entrySet();
        Iterator iterator2 = set2.iterator();
        while(iterator2.hasNext()) {
            Map.Entry mentry2 = (Map.Entry)iterator2.next();
            System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
            System.out.println(mentry2.getValue());
        }
    }
}

What are the methods available with a HashMap object?

  1. void clear(): It removes all the key and value pairs from the specified Map.
  2. Object clone(): It returns a copy of all the mappings of a map and used for cloning them into another map.
  3. boolean containsKey(Object key): It is a boolean function which returns true or false based on whether the specified key is found in the map.
  4. boolean containsValue(Object Value): Similar to containsKey() method, however it looks for the specified value instead of key.
  5. Value get(Object key): It returns the value for the specified key.
  6. boolean isEmpty(): It checks whether the map is empty. If there are no key-value mapping present in the map then this function returns true else false.
  7. Set keySet(): It returns the Set of the keys fetched from the map.
  8. value put(Key k, Value v): Inserts key value mapping into the map. Used in the above example.
  9. int size(): Returns the size of the map – Number of key-value mappings.
  10. Collection values(): It returns a collection of values of map.
  11. Value remove(Object key): It removes the key-value pair for the specified key. Used in the above example.
  12. void putAll(Map m): Copies all the elements of a map to the another specified map.

How to iterate over a HashMap object?

Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    System.out.println(pairs.getKey() + " = " + pairs.getValue());
    it.remove(); // avoids a ConcurrentModificationException
}

How can we removes all the key and value pairs from a HashMap object?

Use its clear() method.

How can we clone a HashMap object?

Use its clone() method.

How can we determine if a HashMap object contains a particular key?

Use its containsKey method. It is a boolean function which returns true or false based on whether the specified key is found in the map.

How can we determine if a HashMap object contains a particular value?

Use its containsValue method.

How can we get the value of a particular key/value pair from a HashMap object?

Use its get() method.

How can we determine if a HashMap object is empty?

Use its isEmpty() method.

How can we obtain a Set of keys of a HashMap object?

Use the keySet() method.

How can we put a new key/value into a HashMap object?

Use the put() method.

How can we determine the number of key/value pairs of a HashMap object?

Use its size() method.

How can we get a collection of values of a HashMap object?

Use its values() method.

How can we remove a key/value pair from a HashMap object?

Use its remove() method.

How can we copies all the elements of a map to the another specified map?

Use its putAll() method.

What is the purpose of the putAll() method of a HashMap object?

Copies all the elements of a map to the another specified map.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License