第七章 09_Map_1
鱼欲遇雨:每日都学习一点,持之以恒,天道酬勤!不能用电脑时,提前补上!(2012.8.31)
如何选择数据结构*
Hash 两者之间
Map接口
1 实现Map接口的类用来存储键———值对。
2 Map接口的实现类有HashMap和TreeMap等。
3 Map类中存储的键---值对通过键来标识,所以键值不能重复.(equals)
Object pub (Object key, Object value);
Object get(Object key);
Object remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
void putAll(Map t);
void clear();
// TestMap.javaimport java.util.*;public class TestMap {public static void main(String args[]) {Map<String, Integer> m1 = new HashMap<String, Integer>();Map<String, Integer> m2 = new TreeMap<String, Integer>();m1.put("one", 1);m1.put("two", 2);m1.put("three", 3);m2.put("A", 1);m2.put("B", 2);//m1.put("one", new Integer(1));//m1.put("two", new Integer(2));//m1.put("three", new Integer(3));//m2.put("A", new Integer(1));//m2.put("B", new Integer(2));System.out.println(m1.size());System.out.println(m1.containsKey("one"));System.out.println(m2.containsValue(/*new Integer(1)*/ 1 ));if(m1.containsKey("two")) {int i = m1.get("two");//int i = ((Integer)m1.get("two")).intValue();System.out.println(i);}Map<String, Integer> m3 = new HashMap<String, Integer>(m1);m3.putAll(m2);System.out.println(m3);}}