String IdentityHashMap vs. HashMap Performance

Identity HashMap is a special implementation in java that compares a reference to objects instead of peers, and also uses the HashCode identifier instead of hashCode. In addition, he uses linear-probe hash tableinstead Entry list.

Map<String,String> map = new HashMap<String,String>(); 

Map<String,String> iMap = new IdentityHashMap<String,String>();

Does this mean that for String keys, IdentifyHashMap will usually be faster if configured correctly?

Base code added

public class Dictionary {

public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader("/usr/share/dict/words"));

    String line;
    ArrayList<String> list = new ArrayList<String>();


    int index=0;
    while( (line = br.readLine()) != null){
        list.add(line);
    }
    System.out.println("list.size() = " + list.size());
    Map<String,Integer> iMap = new IdentityHashMap<String,Integer>(list.size());
    Map<String,Integer> hashMap = new HashMap<>(list.size());

    long iMapTime=0,hashMapTime=0;

    long time=0;
    for(int i=0; i< list.size(); i++){
        time= System.currentTimeMillis();
        iMap.put(list.get(i),i);
        time = System.currentTimeMillis()-time;
        iMapTime += time;
        time= System.currentTimeMillis();
        hashMap.put(list.get(i),i);
        time = System.currentTimeMillis()-time;
        hashMapTime += time;
    }

    System.out.println("iMapTime = " + iMapTime + " hashMapTime = " +hashMapTime);

}

}

Tried a very simple performance control. I read the words of the word (235K) and click on both cards. He prints below.

list.size() = 235886
iMapTime = 101 hashMapTime = 617 

I think this is a very good improvement to ignore if I am not doing something wrong here.

+4
source share
4

IdentityHashMap<String,?>?

IdentityHashMap<String,?> , String.intern() put() get(). ( .)

: @m3th0dman, intern() .

, - - . , - (, , ), .

, , ?

, , put(), . ( , LinkedHashMap iterator() .

IdentityHashMap?

, ( ), equals(). , ThreadLocal, , , - :

public final class ThreadLocal<T> {
   private final IdentityHashMap<Thread,T> valueMap;
   ...
   public T get() {
       return valueMap.get( Thread.currentThread() );
   }
}

, . , ..

+1

IdentityHashMap, .

, , , .

, , .

String t1 = "test";
String t2 = "test";

t1==t2 true.

String t1 = "test";
String t2 = new String("test");

t1==t2 false.

, , , , , IdentityHashMap .

+4

- , , :

public class StringIdentityHashMap extends IdentityHashMap<String, String>
{
    @Override
    public String put(String key, String value)
    {
        return super.put(key.intern(), value.intern());
    }

    @Override
    public void putAll(Map<? extends String, ? extends String> m)
    {
        m.entrySet().forEach(entry -> put(entry.getKey().intern(), entry.getValue().intern()));
    }

    @Override 
    public String get(Object key)
    {
        if (!(key instanceof String)) {
            throw new IllegalArgumentException();
        }
        return super.get(((String) key).intern());
    }

    //implement the rest of the methods in the same way
}

, intern() equals(), , String String, HashMap.

, , , . , ( - JVM, ), , , ( ) equals().

+1

, IdentityHashMap SLOWER. 50% HashMap IdentityHashMap.

IdentityHashMap HashMap , , equals() , HashMap .

0

All Articles