日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Difference Between HashMap and IdentityHashMap--转

發布時間:2025/4/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Difference Between HashMap and IdentityHashMap--转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:https://dzone.com/articles/difference-between-hashmap-and

Most of the time I use HashMap whenever a map kinda object is needed. When reading some blog I came across?IdentityHashMap?in Java. It is good to understand the differences between the two because you never know when you will see them flying across your code and you trying to find out why is ?this kinda Map is used here.

IdentityHashMap as name suggests uses the equality operator(==) for comparing the keys. So when you put any Key Value pair in it the Key Object is compared using == operator.

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

public class IdentityMapDemo {

public static void main(String[] args) {
Map identityMap = new IdentityHashMap();
Map hashMap = new HashMap();
identityMap.put("a", 1);
identityMap.put(new String("a"), 2);
identityMap.put("a", 3);
hashMap.put("a", 1);
hashMap.put(new String("a"), 2);
hashMap.put("a", 3);
System.out.println("Identity Map KeySet Size :: " + identityMap.keySet().size());
System.out.println("Hash Map KeySet Size :: " + hashMap.keySet().size());
}
}

On the other hand HashMap uses equals method to determine the uniqueness of the Key.

k1.equals(k2)

instead of equality operator.

When you run the above code the result will be

Identity Map KeySet Size :: 2

Hash Map KeySet Size :: 1

The Keysize of Identity Map is 2 because here?a?and?new String(“a”)?are considered two different Object. The comparison is done using?==?operator.

For HashMap the keySize is 1 because K1.equals(K2) returns true for all three Keys and hence it keep on removing the old value and updating it with the new one.

These both Maps will behave in same manner if they are used for Keys which are user defined Object and doesn’t overrides equals method.

轉載于:https://www.cnblogs.com/davidwang456/p/6026280.html

總結

以上是生活随笔為你收集整理的Difference Between HashMap and IdentityHashMap--转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。