-
Notifications
You must be signed in to change notification settings - Fork 3
/
JavaHashset.java
46 lines (39 loc) Β· 1.27 KB
/
JavaHashset.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
39
40
41
42
43
44
45
46
// https://www.hackerrank.com/challenges/java-hashset/problem
import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
public class JavaHashset {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
Set<Pair> set = new HashSet<>();
while (queries-- > 0) {
String first = scanner.next();
String second = scanner.next();
Pair pair = new Pair(first, second);
set.add(pair);
System.out.println(set.size());
}
}
private static class Pair {
private final String first;
private final String second;
Pair(String first, String second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return Objects.equals(first, pair.first) &&
Objects.equals(second, pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
}
}