15

Anyone know if Guava has an equivalent version to Functionaljava's HashMap?

ebaxt
  • 8,287
  • 1
  • 34
  • 36

2 Answers2

15

As far as I know, no.

But you can wrap all your keys in Equivalence.Wrapper instances using the Equivalence strategy you need:

Equivalence<K> equiv = ...
Map<Equivalence.Wrapper<K>, V> map = ...

map.put(equiv.wrap(key), value);

Of course this means you need an additional object for every entry in your map. Thus I think a map implementation like you suggest would be nice to have.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
7

I am aware that youre not asking for this specifically, so here goes:
If all you want is a Map with a custom equivalence function you may be able to achieve the same by using the standard TreeMap that takes a custom Comparator.

Johannes
  • 763
  • 3
  • 7
  • Thanks, this was actually what I was looking for, but since it's not actually the answer to the question I decided to accept the other one. – ebaxt Feb 23 '12 at 07:25
  • 6
    Just don't expect everything to work correctly if you do this, as explained in the TreeMap javadocs. – Kevin Bourrillion Feb 23 '12 at 18:00
  • Nice catch. It should still work as expected though. The "only problem" is that the expected behavior is in violation of the `Map` contract. I wonder if its an oversight or on purpose it is so specific about using `equals()`. – Johannes Feb 23 '12 at 22:35
  • 1
    Note that this requires some kind of (sensible) order you can use on your keys – ChristophK Jan 20 '15 at 10:43