Java Map Interface

Java Map Interface
  • The java.util.Map interface represents a mapping between a key and a value.


  • A map contains values on the basis of key, i.e. key and value pair.
  • Each key and value pair is known as an entry.
  • A Map contains unique keys.
  • A Map is useful if you have to search, update or delete elements on the basis of a key.
  • A Map can't be traversed.
  • There are two interfaces for implementing Map in java: 
    • Interfaces: Map and SortedMap, 
    • Classes: HashMap, LinkedHashMap, and TreeMap.


  • Note: The Java Map interface is not a subtype of the Collection interface
Difference between Map Vs HashMap Vs TreeMap:

  • A Map doesn't allow duplicate keys, but you can have duplicate values.
  • HashMap and LinkedHashMap allow null keys and values.
  • HashMap is the implementation of Map, but it doesn't maintain any order.
  • LinkedHashMap is the implementation of Map.
  • It inherits HashMap class and maintains insertion order.
  • TreeMap doesn't allow any null key or value
  • TreeMap is the implementation of Map and SortedMap and maintains ascending order.


  • Why and When to use Maps?
    • Maps are perfect to use for key-value association mapping such as dictionaries.
    • Examples:
      • Error codes (key) and their descriptions (value).
      • Zipcodes (key) and cities (value).
      • Managers (key) and employees (value).
      • Classes (key) and students (value).
How to declare object of Map interface?
  • Syntax:
Map object-name = new HashMap();
Map hm = new HashMap();
  • Example:
Map hm = new HashMap();
Map mapA = new HashMap();

Methods:


How to add new entry into Map? 

public Object put(Object key, Object value): This method is used to insert an entry in this map.
  • Example:
hm.put("a", new Integer(100));
mapA.put("key1", "element 1");

How to Inserting All Elements From Another Map?
public void putAll(Map map): This method is used to insert the specified map in this map.
  • Example:
Map mapB = new HashMap();
mapB.put("keyb1", "valueb1");
mapB.put("keyb2", "valueb2");

mapA.putAll(mapB);

How to Get Elements From a Java Map?
public Object get(Object key):This method is used to return the value for the specified key.
  • Example:
String element1 = (String) mapA.get("key1");
System.out.println(element1);

How to Checking if Map Contains Key?
public boolean containsKey(Object key): This method is used to search the specified key from this map.
  • Example:
boolean hasKey = mapA.containsKey("123");
System.out.println(hasKey);

How to Checking if Map Contains Value?
public boolean containsValue(Object value): This method is used to search the specified value from this map.
  • Example:
boolean hasValue = mapA.containsValue("value 1");
System.out.println(hasValue);
How to Removing Entries From a Java Map
public Object remove(Object key): This method is used to delete an entry for the specified key.
  • Example:
mapA.remove("key1");

How to Iterating the Keys of a Java Map?
public Set keySet(): This method is used to return the Set view containing all the keys.
There are 2 ways to iterate the keys stored in a Java Map.


Using the key Iterator
  • Exmaple:
Iterator iterator = mapA.keySet().iterator();
while(iterator.hasNext(){
Object key = iterator.next();
System.out.println(key);
Object value = mapA.get(key);
System.out.println(value);
}

Using the for-each loop
Exmaple:
for(Object key : mapA.keySet()) {
Object value = mapA.get(key);
System.out.println(key);
System.out.println(value);
}

How to Iterating the Entries of a Java Map? 

public Set entrySet(): This method is used to return the Set view containing all the keys and values.
Example: Set entries = map.entrySet();
Iterator iterator = entries.iterator();
while(iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
}

How to remove all Removing All Entries? 

You can remove all entries in a Java Map using the clear() method.
Example:mapA.clear();

How to Reading Number of Entries in Map?

You can read the number of entries in a Java Map using the size() method.
Example: int entryCount = mapA.size();

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form