• 코드:
​x
 
1
import java.util.*;
2
​
3
class Animal {
4
    String species;
5
    String habitat;
6
    
7
    Animal(String species, String habitat) {
8
        this.species = species;
9
        this.habitat = habitat;
10
    }
11
    
12
    public int hashCode() {
13
        return (species + habitat).hashCode();
14
    }
15
    
16
    public boolean equals(Object obj) {
17
        if (obj instanceof Animal) {
18
            Animal temp = (Animal)obj;
19
            return species.equals(temp.species) && habitat.equals(temp.habitat);
20
        } else {
21
            return false;
22
        }
23
    }
24
}
25
​
26
public class prog {
27
    public static void main(String[] args) {
28
        HashSet<Animal> hs = new HashSet<Animal>();
29
        
30
        hs.add(new Animal("고양이", "육지"));
31
        hs.add(new Animal("고양이", "육지"));
32
        hs.add(new Animal("고양이", "육지"));
33
        
34
        System.out.println(hs.size());
35
    }
36
}
표준입력 & 실행옵션