zoukankan      html  css  js  c++  java
  • java IdentityHashMap 与HashMap

    这两个map的主要区别在于,比较key值什么时候:
    IdentityHashMap我觉得当k1 == k2 时刻key值一样的
    HaspMap觉得k1 == null ? k2 == null:k1.equals(k2)时key值是一样的

    举个样例:
    Integer a = new Integer(123456);
               Integer b = new Integer(123456);
               HashMap hashMap = new HashMap();
               IdentityHashMap identityHashMap = new IdentityHashMap();
               hashMap.put(a,1);
               hashMap.put(b, 2);
               identityHashMap.put(a,1);
               identityHashMap.put(b,2);
               System.out.println(hashMap);
               System.out.println(identityHashMap);

    执行结果:
    P_LOG: {123456=2}
    P_LOG: {123456=1, 123456=2}

    总结:
              HashMap:会使用equals比較key对象
              IdentityHashMap:使用 == 比較key对象
          



    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    关于C++类中的静态数据成员
    关于C++中char,sizeof,strlen,string
    C++学习笔记(7)
    C++学习笔记(6)
    C++学习笔记(指针)
    C++学习笔记(4)
    UVA 10780
    UVA 531
    HDU, 3579 Hello Kiki
    UVA, 10413 Crazy Savages
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4845657.html
Copyright © 2011-2022 走看看