Java HashMap is under java.util. – Java API
1. What is HashMap?
HashMap is an Object that stores both “key/value” as a pairs. In this article, we show how to create a HashMap instance and iterates the HashMap data.
—> put(“key”, “value”); // set the value
—> get(“key”); // get the value
import java.util.HashMap;
import java.util.Map;
// Frank Nandong(08/05/2014)
// franknandong@gmail.com / fndong@wordpress.com
// Task : one key been pick from two keys, get the value.
public class Hasmap_one {
public static String sPickValue = “”;
public static void main(String args[]){
Map mMap = new HashMap();
// add the key and value of the dessert
mMap.put(“banana”, “icecream Banana”);
mMap.put(“apple”, “apple pie”);
// pick the apple key , to get value “apple pie”
// convert object to string by put (String) betwen mMap.get(“apple”)
sPickValue = (String)mMap.get(“apple”);
// output display
System.out.println(“I Pick apple key , the value is : ” + sPickValue);
}
}