Re: Question about HashMap and Map.Entry ...
On 8/30/12 10:46 AM, markspace wrote:
On 8/30/2012 10:39 AM, Andreas Leitgeb wrote:
What I'm missing, however, is:
How would I get the Entry for a given key?
What about:
Long value = hashMap.get( key );
if( value != null )
hashMap.put( key , newValue );
This only updates the value for key if key is already present. I think
that's what you said you are doing. It assumes that "null" is not a
valid value for any key. (That can be changed as well, but it's
basically a scan through the entries at that point.)
In theory, this does more work than it needs to, computing the hash
bucket twice, scanning any collisions on the key's buckets, etc... In
practice, this is fast enough for most operations.
I would like to suggest to the OP to look into Trove
<http://trove4j.sourceforge.net/>, which has support for primitive
collections (longs, rather than Longs), which may be much faster and
more memory efficient, depending on your use-cases.