The best card for interned strings

I have a large number of interned strings (with a small number of possible values, so it makes sense to intern them) that I want to save in Map(for use as a counter).

TreeMapperforms a comparison at each level of the tree, which, I believe, will be associated with the O (n) character mapping. HashMapwill use the hash for the bucket.

Given that I have a small set of interned strings, which means the link can be used to compare equality or ordering (so neither the hash code nor the value needs to be used), I wonder if there is a well-suited structure

(Or, indeed, more specialized, suitable for counting)

My priorities are speed and compact presentation (I deal with a lot of data).

(To avoid comments of "premature optimization", I process about 200 million elements).

+4
source share
1 answer

IdentityHashMap

java.util.IdentityHashMapworks similarly to a class HashMap, but uses identity equality ==and a hash code ( System.identityHashCode) to compare keys. It also has a much smaller memory area since it uses only one array to store both keys and values. Although it ==works as fast as the method System.identityHashCode(Object)has an implementation nativethat carries some overhead (but it is probably the JVM intrinsive).

Hashmap

HashMap, (HashEntry s), - . , String.equals , String.hashCode . " " HashMap . , .

+3

All Articles