一、前言

  前面已经分析了HashMap与LinkedHashMap,现在我们来分析不太常用的IdentityHashMap,从它的名字上也可以看出来用于表示唯一的HashMap,仔细分析了其源码,发现其数据结构与HashMap使用的数据结构完全不同,因为在继承关系上面,他们两没有任何关系。下面,进入我们的分析阶段。

二、IdentityHashMap示例  

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

public class IdentityHashMapTest {
    public static void main(String[] args) {
        Map<String, String> hashMaps = new HashMap<String, String>();
        Map<String, String> identityMaps = new IdentityHashMap<String, String>();
        hashMaps.put(new String("aa"), "aa");
        hashMaps.put(new String("aa"), "bb");
        
        identityMaps.put(new String("aa"), "aa");
        identityMaps.put(new String("aa"), "bb");
        
        System.out.println(hashMaps.size() + " : " + hashMaps);
        System.out.println(identityMaps.size() + " : " + identityMaps);
    }
}
View Code

相关文章: