3

Is there a java.util.Set implementation that does not call an inserted element's hashCode() method?

I have to use some library's class whose hashCode() implementation is ill-behaving: when this hashCode() method is called, it sends an HTTP request....... therefore, putting an instance of that class into a HashSet causes a HTTP request to fire.
I want to reduce interactions with this hashCode() method to a minimum. Therefore, I need a Set implementation that does not exploit its contained element's hashCode() method.

4
  • 4
    TreeSet is the way to go. You need a Comparator instead. Commented Feb 23, 2013 at 21:57
  • 1
    Or extend the dodgy class and override hashCode(), may save you more headaches in the future... Commented Feb 23, 2013 at 21:59
  • 4
    @bmorris591 maybe this class is final and OP can't extend it. Another solution (based on your idea) would be creating a wrapper class for this unknown library class and override the equals and hashCode functions (still, lot of boilerplate code to maintain). Commented Feb 23, 2013 at 22:02
  • Ah, good old decorator pattern... Good call. Don't override just cache the outcomes of hashcode. Commented Feb 23, 2013 at 22:31

1 Answer 1

2

Take a look at the documentation of Object.hashCode() method and Set interface.

Using TreeSet< Comparable > :

import java.util.Set;
import java.util.TreeSet;

public class NoHashCode implements Comparable< NoHashCode >{

   final int value;

   public NoHashCode( int val ) {
      this.value = val;
   }

   @Override public int hashCode() {
      throw new IllegalStateException( "This method must not be called" );
   }

   @Override
   public int compareTo( NoHashCode o ) {
      return this.value - o.value;
   }

   public static void main( String[] args ) {
      Set< NoHashCode > set = new TreeSet<>();
      set.add(  new NoHashCode( 1 ));
      set.add(  new NoHashCode( 2 ));
      set.add(  new NoHashCode( 3 ));
      set.add(  new NoHashCode( 1 )); // '1' is already in set
      System.out.println( set.size());// print 3
   }
}

Using TreeSet< T >(comparator) :

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class NoHashCode {

   final int value;

   public NoHashCode( int val ) {
      this.value = val;
   }

   @Override public int hashCode() {
      throw new IllegalStateException( "This method must not be called" );
   }

   public static void main( String[] args ) {
      Set< NoHashCode > set = new TreeSet<>( new Comparator< NoHashCode >(){
         @Override public int compare( NoHashCode left, NoHashCode right ) {
            return left.value - right.value;
         }});
      set.add(  new NoHashCode( 1 ));
      set.add(  new NoHashCode( 2 ));
      set.add(  new NoHashCode( 3 ));
      set.add(  new NoHashCode( 1 )); // '1' is already in set
      System.out.println( set.size());// print 3
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.