 |
|
|
HashMapTest.java
|
import java.util.HashMap;
import java.util.Map;
/**
*
* Example of HashMap usage.
*
*/
public class HashMapTest {
public static void main(String[] args) {
//define keys
int a = 1;
int b = 2;
//create Map
//notice that the interface is used
Map map = new HashMap();
//assign/insert key-value pairs
map.put(a, "first value");
map.put(b, "second value");
//get the values based on keys
System.out.println("First: "+map.get(a));
System.out.println("Second: "+map.get(2));
/**
* Expected output:
*
* First: first value
* Second: second value
*/
}
}
|
|
|
 |