java中key值可以重复的map:IdentityHashMap



在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:

  1. IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>();  
  2. map.put(newString("xx"),"first");  
  3. map.put(newString("xx"),"second");  
  4. for (Entry<String, Object> entry : map.entrySet()) {  
  5.     System.out.print(entry.getKey() +"    ");  
  6.     System.out.println(entry.getValue());  
  7. }  
  8. System.out.println("idenMap="+map.containsKey("xx"));  
  9. System.out.println("idenMap="+map.get("xx"));  

输出结果是:

  1. xx    first  
  2. xx    second  
  3. idenMap=false  
  4. idenMap=null  


  例子2:

  1. IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>();  
  2.    String fsString =newString("xx");  
  3.    map.put(fsString,"first");  
  4.    map.put(newString("xx"),"second");  
  5.    for(Entry<String, Object> entry : map.entrySet()) {  
  6.        System.out.print(entry.getKey() +"    ");  
  7.        System.out.println(entry.getValue());  
  8.    }  
  9.    System.out.println("idenMap="+map.containsKey(fsString));  
  10.    System.out.println("idenMap="+map.get(fsString));  


输出结果是:

  1. xx    second  
  2. xx    first  
  3. idenMap=true  
  4. idenMap=first  


  例子3:

  1. IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>();  
  2.    String fsString =newString("xx");  
  3.    map.put(fsString,"first");  
  4.    map.put(fsString,"second");  
  5.    for(Entry<String, Object> entry : map.entrySet()) {  
  6.        System.out.print(entry.getKey() +"    ");  
  7.        System.out.println(entry.getValue());  
  8.    }  
  9.    System.out.println("idenMap="+map.containsKey(fsString));  
  10.    System.out.println("idenMap="+map.get(fsString));  


输出结果是:

  1. xx    second  
  2. idenMap=true  
  3. idenMap=second  


例子4:

  1. IdentityHashMap<String,Object> map =newIdentityHashMap<String,Object>();  
  2.    String fsString =newString("xx");  
  3.    String secString =newString("xx");  
  4.    map.put(fsString,"first");  
  5.    map.put(secString,"second");  
  6.    for(Entry<String, Object> entry : map.entrySet()) {  
  7.        System.out.print(entry.getKey() +"    ");  
  8.        System.out.println(entry.getValue());  
  9.    }  
  10.    System.out.println("idenMap="+map.containsKey(fsString));  
  11.    System.out.println("idenMap="+map.get(fsString));  
  12.       
  13.    System.out.println("idenMap="+map.containsKey(secString));  
  14.    System.out.println("idenMap="+map.get(secString));  


输出结果是:

  1. xx    first  
  2. xx    second  
  3. idenMap=true  
  4. idenMap=first  
  5. idenMap=true  
  6. idenMap=second  
Java 中,`Map` 接口的 `put` 方法默认会覆盖已有的键对应的。不过,可以通过一些方式实现向已有 `key` 添加时不覆盖原有。 ### 使用 `IdentityHashMap` `IdentityHashMap` 靠对象来判断 `key` 是否相等,可用于一个 `key` 保存多个 `value` 的情况。可以将每个 `key` 对应的 `value` 存储为一个集合(如 `List`),当向已有 `key` 添加时,将新添加到对应的集合中,而不是覆盖原有。示例代码如下: ```java import java.util.ArrayList; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; public class IdentityHashMapExample { public static void main(String[] args) { Map<Object, List<Object>> identityMap = new IdentityHashMap<>(); String key = new String("exampleKey"); addValue(identityMap, key, "value1"); addValue(identityMap, key, "value2"); System.out.println(identityMap.get(key)); } public static void addValue(Map<Object, List<Object>> map, Object key, Object value) { map.computeIfAbsent(key, k -> new ArrayList<>()).add(value); } } ``` 在上述代码中,`computeIfAbsent` 方法会检查 `key` 是否存在,如果不存在则创建一个新的 `ArrayList`,然后将 `value` 添加到这个集合中。 ### 自定义方法 可以自定义方法来实现向已有 `key` 添加时不覆盖原有。示例代码如下: ```java import java.util.HashMap; import java.util.Map; public class CustomMapMethod { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); addValueWithoutOverwrite(map, "key1", "value1"); addValueWithoutOverwrite(map, "key1", "value2"); System.out.println(map.get("key1")); } public static void addValueWithoutOverwrite(Map<String, String> map, String key, String value) { if (!map.containsKey(key)) { map.put(key, value); } else { String existingValue = map.get(key); map.put(key, existingValue + ", " + value); } } } ``` 在这个示例中,`addValueWithoutOverwrite` 方法会检查 `key` 是否存在,如果不存在则直接 `put`,如果存在则将新与原有拼接起来。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值