1

I would create a Set exactly HashSet to contains only char.for example a,b,c,d,e,f,g... but these chars are not represented by the primitive type but I have an object

public FirstChar{

  private char c;

  public FirstChar(char c){

    this.c = c;

  }
}

Now i want to add the object FirstChar into a set but to avoid repeated elements I have to implement HashCode() and equals()

I know how to implement equals but how can i implement hashcode in the way I could have only one element in the set?

NB. Please don't say me to use Eclipse

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Mazzy
  • 13,354
  • 43
  • 126
  • 207

2 Answers2

3

EDIT: I've just read your comment that you only want one letter in the entire set - which sounds like a very odd requirement, but it's basically fulfilled by something like:

public final class FirstChar {

  private final char c;

  public FirstChar(char c) {
    this.c = c;
  }

  @Override public int hashCode() {
      return 0;
  }

  @Override public boolean equals(Object other) {
      return other instanceof FirstChar;
  }
}

In other words, every instance of FirstChar is deemed equal to every other instance, and they all have the same hash code. As I say, this is really strange... is it definitely what you want?


Original answer

Implementing hashCode() for a value which only logically has a single character is easy:

@Override
public int hashCode() {
    return c; // Use implicit conversion to int
}

Check against the contract of Object.hashCode and you'll find this works fine - assuming your equals method basically just compares values of c. (It's not clear what you meant when you wrote "in the way I could have only one element in the set" - I assume you mean only one element per distinct character.)

However, I'm confused as to what value your FirstChar class provides over just using java.lang.Character. Is there any reason you can't just use a Set<Character>?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon Skeet occasionally wins so many badges that the notifications hide his entire browser. He continues answering questions unhindered. – Paul Bellora Dec 13 '11 at 15:51
  • the reason I don't use wrapper Character is because every letter has to contain a collection of object Person who their surname starts with that letter – Mazzy Dec 13 '11 at 15:52
  • 1
    @Mazzy: That sounds like a very odd requirement - any reason you're not using a `Map>` or something similar (e.g. a `Multimap` from Guava)? – Jon Skeet Dec 13 '11 at 15:55
  • the reason is because I want a collection of element that are not repeated but every of this elements have to contain a list so every element has to be an object to contain a collection – Mazzy Dec 13 '11 at 16:02
  • @JonSkeet but a Map has two fields.the key and the value. Now I need only one field.in the collection we could have more person with the same first letter of surname so Map can't help me – Mazzy Dec 13 '11 at 16:07
1

In any case, from what I can tell, the hashCode method in the Character class simply returns the char as an int.

In your specific case, if you want the set to only contain the first FirstChar added to it, you can make all FirstChars equal to each other:

class FirstChar{
    private char c;
    public FirstChar(char c){
        this.c=c;
    }
    public String toString(){
        return String.valueOf(c);
    }
    public boolean equals(Object o){
        return o instanceof FirstChar;
    }
    public int hashCode(){        
        return 42;
    }
}

But unless you have a very good reason, this doesn't sound like a good idea.

Vlad
  • 18,195
  • 4
  • 41
  • 71