简体   繁体   中英

Java HashObjObjMap<K, V> vs HashMap<K, V>

What is the difference between Koloboke HashObjObj<K, V> and Java util HashMap<K, V> ?

I am aware of the performance that Koloboke provides but there might be instances that K/V turn out to be a Integer/Long. Generally if known HashLongObjMap would be recommended but what happens when K/V come in as generics. From what I understand using HashLongObjMap uses long primitive as the key but what are the differences that come in when HashObjObjMap<Long, V> is used?

Eg:

HashLongObjMap<V> map1 = HashLongObjMaps.newImmutableMap();

Vs

HashObjObjMap<K, V> map2 = HashObjObjMaps.newImmutableMap();

The difference between HashObjObjMap and java.util.HashMap is algorithm and itnernal memory layout. HashObjObjMap is an open-addressing hash table with linear probing, storing keys and values in the same flat Object[] array, in interspersed order: [key1, value1, key2, value2, ...]. Entry objects don't exist, they are created only when required by Map API (ie entrySet() iteration). HashMap is a hash table with separate chaining, keys and values are stored in separate Entry objects.

HashLongObjMap stores keys as primitive long s, HashObjObjMap has ordinary Object keys.

HashObjObjMap<Long, V> cannot call HashLongObjMap internally because they have slightly different contract, eg the latter cannot hold null key. Also I don't see much sense in it, if you need long keys you should just explicitly use HashLongObjMap yourself instead of HashObjObjMap and relying on some implicit "optimizations".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM