-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChallenge_29.java
38 lines (32 loc) · 973 Bytes
/
Challenge_29.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package challenge21_30;
import java.util.HashSet;
import java.util.Set;
/**
* HashSet functionality regarding the hashcode and the equals methods.
*/
public class Challenge_29 {
public static void main( String[] args ) {
Set<Borat> borats = new HashSet<>();
borats.add(new Borat(1, "Kazakhistan"));
borats.add(new Borat(2, "USAndA"));
borats.add(new Borat(2, "England"));
}
static class Borat {
int id;
String country;
public Borat( int id, String country ) {
this.id = id;
this.country = country;
}
@Override
public int hashCode() {
System.out.println("hashcode:"+ this.id+this.country);
return this.id;
}
@Override
public boolean equals( Object obj ) {
System.out.println("equals:"+this.id+this.country);
return ((Borat) obj).country.equals(this.country);
}
}
}