Skip to content

Instantly share code, notes, and snippets.

@kkdeok
Last active March 8, 2019 15:16
Show Gist options
  • Save kkdeok/cf65720aef72164a938301338d88723e to your computer and use it in GitHub Desktop.
Save kkdeok/cf65720aef72164a938301338d88723e to your computer and use it in GitHub Desktop.
class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
}
public String getName() {
return name;
}
// not override equals method.
public boolean equals(Fruit o) {
if (this == o) return true;
if (!(o instanceof Fruit)) return false;
Fruit fruit = (Fruit) o
return Objects.equals(getName(), fruit.getName());
}
@Override
public int hashCode() {
return Objects.hash(getName());
}
}
@Test(expected = AssertionError.class)
public void testEquals() {
Fruit apple1 = new Fruit("apple");
Fruit apple2 = new Fruit("apple");
Set<Fruit> fruits = new HashSet<>();
fruits.add(apple1);
fruits.add(apple2);
assertEquals(1, fruits.size());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment