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.
source
share