Map value
In this chapter you will learn:
Get value set from a Map
values()
method from Map returns all values as a set.
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
//j a va 2 s.c om
public class Main {
public static void main(String[] a) {
Map<String,String> map = new HashMap<String,String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
Collection set = map.values();
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
Get value by key
Once we know a key we can try to get its mapping value by calling
get
method.
import java.util.HashMap;
import java.util.Map;
/*j a v a2s.com*/
public class Main{
public static void main(String[] a) {
Map<String,String> map = new HashMap<String,String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println(map.get("key2"));
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Map