Java includes a map interface. A mapping between a key and a value is represented by the util package. The Collection interface does not have a subtype called Map. As a result, it operates differently than the other collection types. A map comprises distinct keys.
Duplicate keys are not permitted in a Map, although duplicate values are permitted. TreeMap does not support null keys or values, although HashMap and LinkedHashMap do.
Because a Map cannot be navigated, you must convert it to a Set using the keySet() or entrySet() methods.
Map Interface and its Subinterface:
- Map– Unique key to value mapping.
- Map.Entry – Describe an element in a map’s key and value pair. Map’s subinterface is Entry.
- NavigableMap– Extends SortedMap is used to manage a list of elements based on closest match searches.
- SortedMap– Map is extended such that keys are kept in ascending order.
Methods in Map Interface:
Method | Description |
---|---|
V put(Object key, Object value) | It is used to insert an entry into the map. |
void putAll(Map map) | It is used to insert the specified map into the map. |
V putIfAbsent(K key, V value) | If the supplied value does not already exist in the map, it is inserted with the specified key. |
V remove(Object key) | It is used to remove an entry for the given key. |
boolean remove(Object key, Object value) | It removes the supplied values from the map together with the associated specified keys. |
Set keySet() | It returns the Set view containing all the keys. |
boolean containsKey(Object key) | If the key exists in the map, this method returns true; otherwise, it returns false. |
boolean equals(Object o) | It compares the provided Object with the Map. |
V get(Object key) | This method returns the object containing the key’s related value. |
V replace(K key, V value) | It substitutes the provided value for the specified key. |
HashMap class:
- AbstractMap is extended by HashMap, which implements the Map interface.
The map is stored in a hashtable. - This allows get() and put() to execute at the same time.
- This class employs the Hashing technique. Hashing is a method of reducing a huge String to a little String that represents the same String.
- HashMap does not keep track of the order of its elements.
Example
import java.util.*;
// Main class
public class Coderz {
// Main driver method
public static void main(String[] args)
{
// Creating an empty HashMap
Map<String, Integer> map = new HashMap<>();
// Inserting entries in the Map
// using put() method
map.put("CRICKET", 10);
map.put("HOCKEY", 30);
map.put("FOOTBALL", 20);
// Iterating over Map
for (Map.Entry<String, Integer> e : map.entrySet())
// Printing key-value pairs
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
Output
FOOTBALL 20
CRICKET 10
HOCKEY 30
LinkedHashMap class
- HashMap is extended by LinkedHashMap.
- LinkedHashMap is similar to HashMap, except it also maintains the order of elements added to it.
- HashMap has the advantage of speedy insertion, search, and deletion, but it never kept track of the track and order of insertion that LinkedHashMap does, where the components may be accessed in the order in which they were inserted.
Example
// Java Program to Illustrate the LinkedHashmap Class
// Importing required classes
import java.util.*;
// Main class
public class Coderz{
// Main driver method
public static void main(String[] args)
{
// Creating an empty LinkedHashMap
Map<String, Integer> map = new LinkedHashMap<>();
// Inserting pair entries in above Map
// using put() method
map.put("CRICKET", 10);
map.put("HOCKEY", 30);
map.put("FOOTBALL", 20);
// Iterating over Map
for (Map.Entry<String, Integer> e : map.entrySet())
// Printing key-value pairs
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
Output
CRICKET 10
HOCKEY 30
FOOTBALL 20
TreeMap class
- TreeMap class extends AbstractMap and implements NavigableMap interface.
- Depending on which constructor is used, the map is sorted according to the natural ordering of its keys or by a Comparator specified at map formation time.
- This is an effective method of sorting and storing key-value pairs.
- The storage order maintained by the treemap, like any other sorted map, must be consistent with equals, regardless of the specified comparators.
Example
import java.util.*;
// Main class
public class Coderz{
// Main driver method
public static void main(String[] args)
{
// Creating an empty TreeMap
Map<String, Integer> map = new TreeMap<>();
// Inserting custom elements in the Map
// using put() method
map.put("CRICKET", 10);
map.put("HOCKEY", 30);
map.put("FOOTBALL", 20);
// Iterating over Map using for each loop
for (Map.Entry<String, Integer> e : map.entrySet())
// Printing key-value pairs
System.out.println(e.getKey() + " "
+ e.getValue());
}
}
Output
CRICKET 10
FOOTBALL 20
HOCKEY 30
Note: also read about the Java TreeSet class
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment