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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…