HashMap with 8 million entries becomes slow

Hedam :

I have a HashMap with 8 million Point2D's mapping to a LinkedList.

private Map<Point2D,LinkedList<RoadEdge>> adjacencyList;

Everything works as it should, but it takes very long time for me to get data from the HashMap. Is there any alternative, that I can use to optimize getting data out?

I am willing to compromise the time it takes to put() in favor of the time it takes to get().

Eugene :

First thing is to check the distribution of hashcode. First check that, but with a minor change. The hashcode of a Key inside a map is re-hashed internally via:

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

So you should really re-hash your hashcode with this function and than later check how it is distributed.

Generally under a good distribution, your TreeNodes (this is how a bucket is set-uo for many entries) are very fast to be found. For a bucket that would have Integer.MAX_VALUE entries it would take at most 32 steps to find it. That happens because the bin is transformed into a perfectly balanced red-black tree.

Searching inside a Map in general is O(1). Searching a bin with TreeNodes is O(logn). And searching inside a LinkedList is O(n) - much worse then the previous ones.

But that is the time it takes to find a single Entry in the map. If you further need to get the element from the LinkedList that means additional time(worse then finding the entry in the map)

Also for so many entries it is critical to specify the loadFactor and initialCapacity initially (at least initialCapacity), before putting the elements into the map. This is because of re-hashing and moving of elements for another bucket (potentially). If you first put them all and than just try to find them, without altering the Map - this will not be a problem when searching...

But generally unless you measure and measure correctly this might not be the problem you are facing. You might be looking into the wrong direction.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459915&siteId=1