In Java, the Comparator interface allows a client to specify an equals() and compare() method for any type. A Comparator can be passed to most (or maybe all) collections that require sorting, and the methods in the Comparator will be used instead of the methods of the specified class. This allows clients to sort objects in a way that is different from their natural ordering, or even to sort objects that don't have a natural ordering (i.e. don't implement Comparable).

Why isn't there a similar interface for hashing? It could specify two methods, hashCode() and equals(), and be useful for HashSets or HashMaps in the same way Comparators are useful for sorting.

Edit: For those who marked this question a duplicate of [this other question](https://stackoverflow.com/questions/12132880/why-there-is-no-hashable-interface-in-java), I would mention that the other question asks why hashCode is included in every class instead of an interface, while this question is about abstracting the hashing function to allow multiple implementations of it.

Answer Edit: The best methods of getting this functionality seem to be:

-If you're ok using an external library and/or are already using Guava (which is a fantastic library for a lot of reasons), Guava has an [Equivalence](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Equivalence.html) class that allows this.

-If you don't want to use an external library, you can use a custom built adapter, similar to what is done in the top answer on [this SO question](https://stackoverflow.com/questions/15094818/compare-two-java-collections-using-comparator-instead-of-equals).