集合 - Map

Main Methods
Method |
Description |
Object put(Object key, Object value) |
insert object |
Object remove(Object key) |
remove object by key |
Object get(Object key) |
get value by key |
boolean containsKey(Object key) |
search by key |
Set keySet() |
return all keys |
Set entrySet() |
return all key-value pairs (Map.Entry) |
Example
package com.test.collection;
import java.util.Map;
import java.util.HashMap;
public class MapTest {
public static void main(String [] args) {
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(10, "dog");
map.put(11, "cat");
map.put(12, "fish");
map.put(13, "bird");
for(Map.Entry<Integer,String> entry:map.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
}
10 => dog
11 => cat
12 => fish
13 => bird