The Java Map interface

The interface  java.util.Map  Contains the peers < key, value>. Each peer is known as an input. Map contains single-key elements.

The interface Map can be implemented with collections of objects that inherit from it. The most popular uses of Map are HashMap and TreeMap. Each implementation of these object collections is different in terms of the order of the elements during the traverse:

HashTable does not guarantee the order of the elements.
TreeMap ensures the order of items according to the key-value peer path.

Here's an example of how to create an instance of Map:

Map hmap = new HashMap(); 
Map tmap = new TreeMap();

The Map

1. public Object put(object key,Object value):
Appends a key associated with its value.

Map hmap = new HashMap(); 
hmap.put(1, "e1");
hmap.put(2, "e2");
hmap.put(3, "e3");
2. public void putAll(Map map)
Insert a specific map into this map.

Map hmap2 = new HashMap(); 
hmap2.put(4, "e4");
hmap2.put(5, "e5");
hmap.putAll(hmap2);

3. public Object get(object key):
To retrieve a value with its key, we want to get the key value 2 from the previous example:

String e2 = (String) hmap.get(2); 
4. public Object remove(object key):
Removes an entry from a specific key.

hmap.remove(1); 
5.  public boolean containsKey(Object key)
Search for a specific key in this map.

6.  public boolean containsValue(Object value)
Search for a specific value in this map.

7.  public Set keySet():
Returns the keyset, keySet() is useful when browsing the list:

for(Object key : hmap.keySet()) {
Object value = hmap.get(key);
System.out.println(value);
}
8.  public Set entrySet():
Returns the set of keys and values, entrysSet() is useful when browsing the list:

for (Map.Entry entry: hmap.entrySet()
{
System.out.println(entry.getKey() + "-" + entry.getValue());
}

Generic map

By default, you can put any type in your map, but you can also limit the type of objects to iterate through keys and values without using the cast:

Map< Integer, String> hmap = new HashMap< Integer, String> (); 
This map only accepts objects Integer for keys and String for values. The advantage of genericity is the access to elements without casting:

import java.util.Hashtable; 
import java.util.Map;

public class Example {

public static void main(String a[]){
//creation
Map ht = new Hashtable();

//add the key-values
ht.put(1, "java");
ht.put(2, "C");
ht.put(3, "C++");

for (Map.Entry entry: ht.entrySet())
{
int key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "-" + value);
}
}
}
Results:

3-C++
2-C
1-java
References:
Java Collections - Map
Java Map Interface
Iterate over each Entry in a Map