zoukankan      html  css  js  c++  java
  • 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:

    IdentityHashMap<String,Object> map =new IdentityHashMap<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")); 
    

      输出结果是:

    xx    first  
    xx    second  
    idenMap=false  
    idenMap=null 
    

      例子2:

    IdentityHashMap<String,Object> map =new IdentityHashMap<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));
    

      输出结果是:

    xx    second  
    xx    first  
    idenMap=true  
    idenMap=first
    

      例子3:

    IdentityHashMap<String,Object> map =new IdentityHashMap<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));
    

      输出结果是:

    xx    second  
    idenMap=true  
    idenMap=second
    

      例子4:

    IdentityHashMap<String,Object> map =new IdentityHashMap<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));  
    

      输出结果是:

    xx    first  
    xx    second  
    idenMap=true  
    idenMap=first  
    idenMap=true  
    idenMap=second 
    

      例子5:

    IdentityHashMap<String,Object> map =new IdentityHashMap<String,Object>();  
    map.put("xx","first");  
    map.put("xx","second");  
    for(Entry<String, Object> entry : map.entrySet()) {  
        System.out.print(entry.getKey() +"    ");  
        System.out.println(entry.getValue());  
    }  
    

      输出结果是:

    xx    second
    

      可以看到,在IdentityHashMap中,是判断key是否为同一个对象,而不是普通HashMap的equals方式判断。

    参考:http://blog.csdn.net/stoneok07/article/details/7262676

  • 相关阅读:
    Tensorflow的认识
    机器学习中的重点数学知识
    深度学习TensorFlow常用函数
    18、OpenCV Python 简单实现一个图片生成(类似抖音生成字母人像)
    17、OpenCV Python 数字验证码识别
    django contenttype 表应用
    contentType 应用,(表中数据大量存在外键时使用)
    django 组件拾遗
    rest_framework 的验证,权限,频率
    restframework CBV试图的4种方式
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/7309522.html
Copyright © 2011-2022 走看看