idea for more efficient HashMap

R

Roedy Green

Inside HashMap are little glue Entry objects that point to the key and
value.

What if you could implement an interface on your objects so that
HashMap could use them directly without separate key or Entry glue?.

e.g. getKey()
getPrev()
getNext()
setPrev()
setNext()

One drawback would be your objects could live on only one such
space-efficient HashMap.
 
V

Volker Borchert

Roedy said:
Inside HashMap are little glue Entry objects that point to the key and
value.

What if you could implement an interface on your objects so that
HashMap could use them directly without separate key or Entry glue?.

e.g. getKey()
getPrev()
getNext()
setPrev()
setNext()

One drawback would be your objects could live on only one such
space-efficient HashMap.

Use linear probing instead of chained buckets. IdentityHashMap does so.
 
R

Robert Klemme

Inside HashMap are little glue Entry objects that point to the key and
value.

What if you could implement an interface on your objects so that
HashMap could use them directly without separate key or Entry glue?.

e.g. getKey()
getPrev()
getNext()
setPrev()
setNext()

One drawback would be your objects could live on only one such
space-efficient HashMap.

I've once implemented a hash map which uses double hashingand uses a
single Object[] for storage of keys and values. It creates Entry
instances on the fly while iterating. We did this to get rid of a few
hundred thousand Entry instances and improve GC behavior of the
application. Works pretty good.

http://en.wikipedia.org/wiki/Double_hashing

Side benefit of that implementation was that you get
ConcurrentModificationException only if the map needed to be resized as
part of an insert operation.

I cannot share this implementation here as it is proprietary code. But
you should pretty much have everything to implement it yourself. If you
do it do not forget to create meaningful unit tests.

Kind regards

robert
 
K

Kevin McMurtrie

Roedy Green said:
Inside HashMap are little glue Entry objects that point to the key and
value.

What if you could implement an interface on your objects so that
HashMap could use them directly without separate key or Entry glue?.

e.g. getKey()
getPrev()
getNext()
setPrev()
setNext()

One drawback would be your objects could live on only one such
space-efficient HashMap.

I've done this when efficiency demanded it. The downside is that you
can't implement java.util.Map or java.util.Dictionary because of the way
put(K,V) is declared.

There's no need for hashed storage in Maps. Hashing is a good general
purpose performer but special cases can do better. Maps having 1 to 10
elements may perform better with the keys and values interleaved in one
array. A search tree (TreeMap) can perform better when keys are large.
 
R

Robert Klemme

I've done this when efficiency demanded it. The downside is that you
can't implement java.util.Map or java.util.Dictionary because of the way
put(K,V) is declared.

Why that? I actually have done that implementation (see above) and it is consistent with the Map interface.
I will not see posts from Google because I must filter them as spam

That might be a mistake - you'll might lose valuable feedback that way.

Kind regards

robert
 
L

Lars Enderin

2013-01-16 23:31, Robert Klemme skrev:
Why that? I actually have done that implementation (see above) and it is consistent with the Map interface.


That might be a mistake - you'll might lose valuable feedback that way.

He will not see your post then...
 
K

Kevin McMurtrie

Lars Enderin said:
2013-01-16 23:31, Robert Klemme skrev:

He will not see your post then...

The Google filter is real. Google doesn't maintain their systems so
they're employed by Chinese crime gangs to flood many Usenet groups.

My original point is that you can't gracefully enforce insertion of an
object having the key, value, and collision link together when put(K,V)
takes two arguments. It's unfortunate that Dictionary defines setter
methods. There are so many cases where I want a widely supported
implementation of V get(K) without the other things. Maybe if interface
compatibility was more flexible it wouldn't be a problem.
 
R

Robert Klemme

As I said above... :)
The Google filter is real. Google doesn't maintain their systems so
they're employed by Chinese crime gangs to flood many Usenet groups.

My Usenet provider does a pretty decent job filtering spam for around 10
EUR / year. So there are definitively ways to handle that.
My original point is that you can't gracefully enforce insertion of an
object having the key, value, and collision link together when put(K,V)
takes two arguments.

What exactly do you mean by "collision link"?
It's unfortunate that Dictionary defines setter
methods. There are so many cases where I want a widely supported
implementation of V get(K) without the other things.

Are you referring to the setter of Map.Entry<K,V>?

Kind regards

robert
 
E

Eric Sosman

[...]
My original point is that you can't gracefully enforce insertion of an
object having the key, value, and collision link together when put(K,V)
takes two arguments. It's unfortunate that Dictionary defines setter
methods. There are so many cases where I want a widely supported
implementation of V get(K) without the other things. Maybe if interface
compatibility was more flexible it wouldn't be a problem.

One approach would be to write a class implementing the
Map<K,V> interface, but whose put(K,V) method throws an
exception (just as an UnmodifiableMap's does). Sketch:

interface Mappable<K,V> {
K getKey();
V getValue();
Mappable<K,V> getNext();
// ...
}

class Mapping<K,V> implements Map<K,V> {
@Override
public V put(K key, V value) {
throw new UnsupportedOperationException();
}

// Not an @Override
public void put(Mappable<K,V> entry) {
// ...
}

// ...
}

True, run-time instead of compile-time detection of the use
of an unsupported method is not exactly graceful, but there's
certainly precedent. (Maybe you can @deprecate the put(K,V)
method; off-hand I don't know whether that works with a method
that's not deprecated by its interface -- and even if you can,
that only provides a compile-time guard for explicit uses of
the Mapping class, not for references via its Map interface.)
 
R

Roedy Green

Google doesn't maintain their systems so
they're employed by Chinese crime gangs to flood many Usenet groups.

Nobody "maintains" (moderates) posts entering the newsgroups via other
streams either. The only reason to deprecate Google Groups is they
are more accessible for newbies. For me, they are the ones I want to
talk to most.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 
A

Arne Vajhøj

Nobody "maintains" (moderates) posts entering the newsgroups via other
streams either. The only reason to deprecate Google Groups is they
are more accessible for newbies. For me, they are the ones I want to
talk to most.

I am sure that they frequently click on your links.

Arne
 
G

Gene Wirchenko

I am sure that they frequently click on your links.

I have clicked on a link of Roedy's when the link was of a
subject of interest to me. I like to have helpful things available.

Sincerely,

Gene Wirchenko
 
A

Arne Vajhøj

I have clicked on a link of Roedy's when the link was of a
subject of interest to me. I like to have helpful things available.

If you liked the content then good for you.

As you probably have noticed, then there is not universal agreement
on the quality of those pages.

I would go to Wikipedia instead.

Arne
 
G

Gene Wirchenko

On 1/30/2013 2:34 PM, Gene Wirchenko wrote:
[snip]
I have clicked on a link of Roedy's when the link was of a
subject of interest to me. I like to have helpful things available.

If you liked the content then good for you.

As you probably have noticed, then there is not universal agreement
on the quality of those pages.

That is true of the Internet in general. Planning on giving up
on the Net?
I would go to Wikipedia instead.

Any port in a storm.

Sincerely,

Gene Wirchenko
 
A

Arne Vajhøj

On 1/30/2013 2:34 PM, Gene Wirchenko wrote:
[snip]
I have clicked on a link of Roedy's when the link was of a
subject of interest to me. I like to have helpful things available.

If you liked the content then good for you.

As you probably have noticed, then there is not universal agreement
on the quality of those pages.

That is true of the Internet in general. Planning on giving up
on the Net?

If there were a better network: yes. But there isn't.

There are better places than Roedy's site for information
about Java.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top