微信登录

python 遍历map

map jdk1.2有

类型 HashMap LinkedHashMap TreeMap Hashtable Properties CurrentHashMap
实现接口 Map Map Map Map
列数 双列集合 双列集合 双列集合 双列集合 双列集合 双列集合
数据 key-value key-value key-value key-value key-value key-value
Jdk出现版本 1.2 1.4 1.2 1.0
顺序 无顺序 有顺序
适用场景 访问查询遍历多,插入删除少 配置文件,key、value是string类型
效率
相等判断 重写hashcode()、equal() 重写hashcode()、equal() 不需要
名字
数据单位 [k-v][to] [to][k-v][to] 红黑树
数据单位 [Entry][to] [to][Entry][to] 红黑树
数据原理 [Entry][to][Entry][to] [to][Entry][to][to][Entry][to]
null 可以出现null,null 不可以出现null,null
初始化 Map m = new HashMap(); TreeMap map = new TreeMap(); Properties pros = new Properties();
查规则 Object get(Object key) 同左 同左 同左 同左 同左
xx.get() 同左 同左 同左 同左 同左
增规则 Object put(Object key,Object value) 同左 同左 同左 同左 同左
xx.put() 同左 同左 同左 同左 同左
全增规则 void putAll(Map m) 同左 同左 同左 同左 同左
全增 xx.putAll() 同左 同左 同左 同左 同左
删规则 Object remove(Object key) 同左 同左 同左 同左 同左
xx.remove() 同左 同左 同左 同左 同左
全删规则 void clear() 同左 同左 同左 同左 同左
全删 xx.clear() 同左 同左 同左 同左 同左
改规则 E set(int index, E element) 同左 同左 同左 同左 同左
xx.set() 同左 同左 同左 同左 同左
包含指定的key boolean containsKey(Object key) 同左 同左 同左 同左 同左
包含指定的value boolean containsValue(Object value) 同左 同左 同左 同左 同左
个数 int size() 同左 同左 同左 同左 同左
是否为空 boolean isEmpty() 同左 同左 同左 同左 同左
判断当前map和参数对象obj是否相等 boolean equals(Object obj) 同左 同左 同左 同左 同左
遍历-key构成Set集合 for (String key : map.keySet()) {System.out.println("key=" + key);} 同左 同左 同左 同左 同左
遍历-value构成Collection集合 for (String value : map.values()) {System.out.println("value=" + value);} 同左 同左 同左 同左 同左
遍历-key-value对构成Set集合 for (String key : map.keySet()) {System.out.println("key=" + key+" value="+map.get(key));} 同左 同左 同左 同左 同左
遍历-key-value-iterator Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, String> entry = iterator.next();System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());} 同左 同左 同左 同左 同左
遍历-不用泛型 Iterator iterator1 = map.entrySet().iterator();while (iterator1.hasNext()) {Map.Entry entry1 = (Map.Entry) iterator1.next();System.out.println("key=" + entry1.getKey() + " value=" + entry1.getValue());} 同左 同左 同左 同左 同左
排序 实现排序遍历,自然排序、定制排序
扩容
线程 未实现同步,线程不安全 实现了同步,线程安全
Jdk7 实例化以后,底层创建了长度是16的一维数组Entry[] table
Jdk8 Node[],put()后创建了长度是16
python 遍历map