java中key值可以重复的map:IdentityHashMap
2016-10-17 12:02
357 查看
在java中,有一种key值可以重复的map,就是IdentityHashMap。在IdentityHashMap中,判断两个键值k1和 k2相等的条件是 k1 == k2 。在正常的Map 实现(如 HashMap)中,当且仅当满足下列条件时才认为两个键 k1 和 k2 相等:(k1==null ? k2==null : e1.equals(e2))。
IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。该类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定,该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。
具体说明,详见:http://download.oracle.com/javase/6/docs/api/java/util/IdentityHashMap.html
http://www.cjsdn.net/Doc/JDK50/java/util/IdentityHashMap.html
在使用IdentityHashMap有些需要注意的地方:
例子1:
输出结果是:
例子2:
输出结果是:
例子3:
输出结果是:
例子4:
输出结果是:
在java中,有一种key值可以重复的map,就是IdentityHashMap。在IdentityHashMap中,判断两个键值k1和 k2相等的条件是 k1 == k2 。在正常的Map 实现(如 HashMap)中,当且仅当满足下列条件时才认为两个键 k1 和 k2 相等:(k1==null ? k2==null : e1.equals(e2))。
IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。该类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定,该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。
具体说明,详见:http://download.oracle.com/javase/6/docs/api/java/util/IdentityHashMap.html
http://www.cjsdn.net/Doc/JDK50/java/util/IdentityHashMap.html
在使用IdentityHashMap有些需要注意的地方:
例子1:
[java] view plain copy IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>(); map.put(newString("xx"),"first"); map.put(newString("xx"),"second"); for (Entry<String, Object> entry : map.entrySet()) { System.out.print(entry.getKey() +" "); System.out.println(entry.getValue()); } System.out.println("idenMap="+map.containsKey("xx")); System.out.println("idenMap="+map.get("xx")); |
[plain] view plain copy xx first xx second idenMap=false idenMap=null |
[java] view plain copy IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>(); String fsString =newString("xx"); map.put(fsString,"first"); map.put(newString("xx"),"second"); for(Entry<String, Object> entry : map.entrySet()) { System.out.print(entry.getKey() +" "); System.out.println(entry.getValue()); } System.out.println("idenMap="+map.containsKey(fsString)); System.out.println("idenMap="+map.get(fsString)); |
[plain] view plain copy xx second xx first idenMap=true idenMap=first |
[java] view plain copy IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>(); String fsString =newString("xx"); map.put(fsString,"first"); map.put(fsString,"second"); for(Entry<String, Object> entry : map.entrySet()) { System.out.print(entry.getKey() +" "); System.out.println(entry.getValue()); } System.out.println("idenMap="+map.containsKey(fsString)); System.out.println("idenMap="+map.get(fsString)); |
[plain] view plain copy xx second idenMap=true idenMap=second |
[java] view plain copy IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>(); String fsString =newString("xx"); String secString =newString("xx"); map.put(fsString,"first"); map.put(secString,"second"); for(Entry<String, Object> entry : map.entrySet()) { System.out.print(entry.getKey() +" "); System.out.println(entry.getValue()); } System.out.println("idenMap="+map.containsKey(fsString)); System.out.println("idenMap="+map.get(fsString)); System.out.println("idenMap="+map.containsKey(secString)); System.out.println("idenMap="+map.get(secString)); |
[plain] view plain copy xx first xx second idenMap=true idenMap=first idenMap=true idenMap=second |
相关文章推荐
- key可以重复的Map集合:IdentityHashMap
- Java中key可以重复的Map集合:IdentityHashMap(转)
- key可以重复的Map集合:IdentityHashMap
- HashMap,HashTable,synchronizedMap,ConcurrentHashMap,TreeMap,IdentityHashMap的比较分析
- IdentityHashMap :key值可以重复的Map集合
- java中key值可以重复的map:IdentityHashMap
- HashMap的key是否可以重复
- key可以重复的Map集合:IdentityHashMap
- Java IdentityHashMap重复值的输出问题
- java中key值可以重复的map:IdentityHashMap
- java中key值可以重复的map:IdentityHashMap
- java中key值可以重复的map---IdentityHashMap
- Mybatis中resultMap是否可以共享(每个mapper.xml中都要重复定义相同的resultMap吗)
- key可以重复的Map集合:IdentityHashMap
- java中key值可以重复的map:IdentityHashMap
- java中key值可以重复的map:IdentityHashMap
- Map子类重要源码分析对比&HashMap&HashTable&TreeMap&IdentityHashMap&WeakHashMap
- IdentityHashMap——允许键重复
- Java集合框架之Map--IdentityHashMap,WeakHashMap,EnumMap源码分析
- Java 取集合的差集,list集合或者map集合,都可以取差集。使用guava工具类来做。而不是重复的造轮子。