Last active
March 8, 2019 15:16
-
-
Save kkdeok/cf65720aef72164a938301338d88723e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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