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

    java IdentityHashMap 与HashMap

    这两个map的主要区别在于,比较key值什么时候:
    IdentityHashMap我觉得当k1 == k2 时刻key值一样的,比较的是引用
    HaspMap觉得k1 == null ? k2 == null:k1.equals(k2)时key值是一样的,比较的是值
     
    举个样例:
    Integer a = new Integer(150);
               Integer b = new Integer(150);
               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: {150=2}
    P_LOG: {150=1, 150=2}
     
    总结:
              HashMap:会使用equals比较key对象
              IdentityHashMap:使用 == 比较key对象
  • 相关阅读:
    性能百万/s:腾讯轻量级全局流控方案详解
    Swagger2
    shiro 入门
    01、单例模式
    02、工厂方法
    04、模板模式
    13、Adapter 适配器
    14、迭代器
    Java 面向切面 AOP
    spring boot 中使用 Redis 与 Log
  • 原文地址:https://www.cnblogs.com/polo/p/13793453.html
Copyright © 2011-2022 走看看