sumsrv
sumsrv

Reputation: 674

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();

Upvotes: 1

Views: 470

Answers (1)

leventov
leventov

Reputation: 15303

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 (i. e. entrySet() iteration). HashMap is a hash table with separate chaining, keys and values are stored in separate Entry objects.

HashLongObjMap stores keys as primitive longs, HashObjObjMap has ordinary Object keys.

HashObjObjMap<Long, V> cannot call HashLongObjMap internally because they have slightly different contract, e. g. 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".

Upvotes: 1

Related Questions