Easy Tutorial
❮ String Format Arrays Insert ❯

Java Map Interface

The Map interface maps keys to values. Values can be retrieved using their corresponding keys.

Number Method Description
1 void clear() <br>Removes all mappings from this map (optional operation).
2 boolean containsKey(Object k) <br>Returns true if this map contains a mapping for the specified key.
3 boolean containsValue(Object v) <br>Returns true if this map maps one or more keys to the specified value.
4 Set entrySet() <br>Returns a Set view of the mappings contained in this map.
5 boolean equals(Object obj) <br>Compares the specified object with this map for equality.
6 Object get(Object k) <br>Returns the value to which this map maps the specified key, or null if the map contains no mapping for this key.
7 int hashCode() <br>Returns the hash code value for this map.
8 boolean isEmpty() <br>Returns true if this map contains no key-value mappings.
9 Set keySet() <br>Returns a Set view of the keys contained in this map.
10 Object put(Object k, Object v) <br>Associates the specified value with the specified key in this map (optional operation).
11 void putAll(Map m) <br>Copies all of the mappings from the specified map to this map (optional operation).
12 Object remove(Object k) <br>Removes the mapping for this key from this map if it is present (optional operation).
13 int size() <br>Returns the number of key-value mappings in this map.
14 Collection values() <br>Returns a Collection view of the values contained in this map.

Example

The following example demonstrates the functionality of the Map interface:

import java.util.*;

public class CollectionsDemo {

   public static void main(String[] args) {
      Map m1 = new HashMap(); 
      m1.put("Zara", "8");
      m1.put("Mahnaz", "31");
      m1.put("Ayan", "12");
      m1.put("Daisy", "14");
      System.out.println();
      System.out.println(" Map Elements");
      System.out.print("\t" + m1);
   }
}

The output of the above example is:

Map Elements
        {Mahnaz=31, Ayan=12, Daisy=14, Zara=8}
❮ String Format Arrays Insert ❯