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.
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. |
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());
}
}
FOOTBALL 20
CRICKET 10
HOCKEY 30
// 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());
}
}
CRICKET 10
HOCKEY 30
FOOTBALL 20
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());
}
}
CRICKET 10
FOOTBALL 20
HOCKEY 30
Note: also read about the Java TreeSet class
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.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…