3

If I need to use InfoName as the key of a HashMap, do I need to define my own hashCode() and equals() method? I think it's not necessary, since the String name variable will be enough to make sure each object of InfoName is different.

public class InfoName {
    enum Type {
        company, product;
    }

    public String name;
    public Type type;

    public InfoName(String name, Type type) {
        this.name = name;
        this.type = type;
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
marlon
  • 6,029
  • 8
  • 42
  • 76
  • 2
    How is the map going to know to use the name variable if you don't define equals() and hashCode() to tell it to do so? – azurefrog Jul 15 '15 at 18:13
  • 3
    If you're going to use an `InfoName` instance as the key then, yes, you need to redefine the `hashCode` and `equals`. but since you only seem to care about the `name` variable, why not use *that* as the key? Then you don't need to worry about any of this. – sstan Jul 15 '15 at 18:15

3 Answers3

2

the String "name" variable will be enough to make sure each object of InfoName is different

If you only want to use the name in the InfoName, then just make String type name as the key as it already override equals() and hashCode() .

OR

You need to override equals() and hashCode() in InfoName class, else how would JVM knows on which attribute/criteria you are using for hashing and equality check.

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
  • I need to know its type for each name, so I need to bundle the type and name together as an object. For example, some names will have the same type, and based on this, I want to count the number of names with the same type. – marlon Jul 15 '15 at 18:27
  • @martin so your whole goal is to find the count of names with the same type? – AnkeyNigam Jul 15 '15 at 18:30
  • print all the names with counts of each, but at the same time print counts of each type. – marlon Jul 15 '15 at 18:32
  • Will `name` be always unique for all `InfoName` objects – AnkeyNigam Jul 15 '15 at 18:37
  • It could be that the same name can belong to different types. – marlon Jul 15 '15 at 18:46
  • Then in that case `InfoName ` as key also wont solve your problem , coz combination of `name` / `type` should be unique else the `value` will be replaced with the new `value` if the same `name` comes again. But a t the same time you also want the names with counts of each. So i would rather prefer a `List` of `InfoName ` type and then iterate over it to have your logics. – AnkeyNigam Jul 15 '15 at 18:54
0

If you are sure to have InfoName as key you need to override both. You an have something like

public class Test {
    enum Type {
        company, product;
    }

    public String name;
    public Type type;

    public Test(String name, Type type) {
        this.name = name;
        this.type = type;
    }

    @Override
    public boolean equals(Object o) {//or do what you like
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Test test = (Test) o;

        if (!name.equals(test.name)) return false;
        if (type != test.type) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + type.hashCode();
        return result;
    }
}

Basically your editor provides features of overriding hashcode and equals. Have a look here Why do I need to override the equals and hashCode methods in Java?enter link description here

Community
  • 1
  • 1
Dhruv Pal
  • 849
  • 2
  • 10
  • 25
0

I think it's not necessary, since the String "name" variable will be enough to make sure each object of InfoName is different.

I would recommend not to use name as the hash key because it seems a bad candidate. I mean could you have multiple objects with the same product name? In that case you would have many collisions.

Cratylus
  • 52,998
  • 69
  • 209
  • 339