A HashMap is a data table with a special structure that allows data objects to be found quickly. The position of a data object is defined by a hash function. In the HashMap, the data is stored as key pairs. The values can be accessed via the key. Functions for adding, deleting and modifying elements can be accessed via a HashMap object.
import java.util.HashMap;
HashMap<String, Integer> map = new HashMap<String, Integer>();
import java.util.HashMap;
public class HashMapBasics {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("ProductA", 1);
map.put("ProductB", 2);
map.put("ProductC", 3);
map.put("ProductD", 4);
for(String name: map.keySet()) {
System.out.println(name + ": " + map.get(name));
}
map.put("ProductC", 8);
System.out.println("New value for ProductC : " + map.get("ProductC"));
}
}
ProductC: 3
ProductD: 4
ProductA: 1
ProductB: 2
New value for ProductC : 8