Menu
Topics Index
...
`


Collections Framework > Map Interfaces >
Siva Nookala - 20 Feb 2017
A Map is an object that maps keys to values. A map cannot contain duplicate keys, each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.

The Java platform contains three general-purpose Map implementations: Java HashMap Implementation, TreeMap In Java - java.util.TreeMap, and LinkedHashMap In Java with Code Example.

Map Methods :
MethodDescription
void clear()Removes all of the mappings from this map (optional operation).
boolean containsKey(Object k)Returns true if this map contains a mapping for the specified key.
boolean containsValue(Object v)Returns true if this map maps one or more keys to the specified value.
Set entrySet()Returns a Set view of the mappings contained in this map.
boolean equals(Object obj)Compares the specified object with this map for equality.
Object get(Object k)Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
int hashCode()Returns the hash code value for this map.
boolean isEmpty()Returns true if this map contains no key-value mappings.
Set keySet()Returns a Set view of the keys contained in this map.
Object put(Object k, Object v)Associates the specified value with the specified key in this map (optional operation).
void putAll(Map m)Copies all of the mappings from the specified map to this map (optional operation).
Object remove(Object k)Removes the mapping for a key from this map if it is present (optional operation).
int size()Returns the number of key-value mappings in this map.
Collection values()Returns a Collection view of the values contained in this map.

Map
import java.util.HashMap;
import java.util.Map;

class MapTest
{
    public static void main(String arg[])
    {
        Map<String, String> m = new HashMap<String, String>();
        Map<String, String> m1 = new HashMap<String, String>();
        System.out.println("Map objects equal : " + m.equals(m1)); // Equals method
        
        m.put("8", "Prakash"); // Put method
        m.put("31", "Shabaz");
        m.put("12", "Raj");
        m.put("14", "Praveen");
        m.put("5", "Gopi");
        
        System.out.println("Map Elements : " + m); // Display map elements
        System.out.println("Size of Map : " + m.size());
        System.out.println("Key contains : " + m.containsKey("31")); // LINE A
        System.out.println("Value contains : " + m.containsValue("Raj")); // LINE B
        System.out.println("Gets the value of key : " + m.get("14")); // LINE C
        System.out.println("Hash code for map : " + m.hashCode());
        System.out.println("Is map empty : " + m.isEmpty());
        System.out.println("Removes the key value : " + m.remove("12")); // LINE D
        System.out.println("Key set : " + m.keySet()); // LINE E
        System.out.println("Collection values : " + m.values()); // LINE F
        
        m1.putAll(m); // LINE G
        m.clear();
        System.out.println("Map after clear : " + m);
        System.out.println("All data of m is put into m1 map : " + m1);
        System.out.println("Entry set : " + m1.entrySet());    
    }
}
OUTPUT

Map objects equal : true
Map Elements : {5=Gopi, 31=Shabaz, 8=Prakash, 14=Praveen, 12=Raj}
Size of Map : 5
Key contains : true
Value contains : true
Gets the value of key : Praveen
Hash code for map : 867799845
Is map empty : false
Removes the key value : Raj
Key set : [5, 31, 8, 14]
Collection values : [Gopi, Shabaz, Prakash, Praveen]
Map after clear : {}
All data of m is put into m1 map : {5=Gopi, 31=Shabaz, 8=Prakash, 14=Praveen}
Entry set : [5=Gopi, 31=Shabaz, 8=Prakash, 14=Praveen]

DESCRIPTION

In this program, all the Map methods are applied on map object. The map is of type HashMap. It contains five elements.

THINGS TO TRY
  • At LINE A replace 31 by 5 to see that output is true. Which means key 5 is present in map. Enter any other key which is not in the map to return false. The same thing repeat for value at LINE B.
  • At LINE C give any key to return the value associated with that key. It returns null if the key does not exist in map.
  • at LINE D, remove is used to delete the element from map. Try with key 5.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App