Skip to content

Equivalence-based Sets & Maps #576

Closed
@gissuebot

Description

@gissuebot

Original issue created by SeanPFloyd on 2011-03-21 at 11:17 AM


Given the Equivalence interface, which has these methods:

  boolean equivalent(@Nullable T a, @Nullable T b);

  int hash(@Nullable T t);

I am surprised that there aren't any Sets / Maps based on Equivalences.

It should be pretty simple to implement an Equivalence-based HashMap / LinkedHashMap / HashSet / LinkedHashSet, just replace all calls to

a) a.hashCode() with equivalence.hash(a)
b) a.equals(b) with equivalence.equivalent(a, b)

And I'd expect to see factory methods like this:

  • Map<A,B> Maps.newEquivalenceHashMap(Equivalence<? super A>)
  • Map<A,B> Maps.newEquivalenceLinkedHashMap(Equivalence<? super A>)
  • Set<A> Sets.newEquivalenceHashSet(Equivalence<? super A>)
  • Set<A> Sets.newEquivalenceLinkedHashSet(Equivalence<? super A>)

Or perhaps:

  • Map<A,B> Equivalences.newHashMap(Equivalence<? super A>)
  • Map<A,B> Equivalences.newLinkedHashMap(Equivalence<? super A>)
  • Set<A> Equivalences.newHashSet(Equivalence<? super A>)
  • Set<A> Equivalences.newLinkedHashSet(Equivalence<? super A>)

Obviously, the performance of these Classes would depend on the quality of the Equivalence implementation (Hash distribution etc), but without these Classes / Methods I don't really see the point in the Equivalence interface.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions