-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathPair.java
More file actions
49 lines (39 loc) · 1.1 KB
/
Pair.java
File metadata and controls
49 lines (39 loc) · 1.1 KB
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
47
48
49
package com.example.task01;
import java.util.Objects;
import java.util.function.BiConsumer;
public class Pair<T, U> {
private final T first;
private final U second;
private Pair(T first, U second) {
this.first = first;
this.second = second;
}
public T getFirst() {
return first;
}
public U getSecond() {
return second;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Pair<?, ?> pairedObj = (Pair<?, ?>) obj;
return Objects.equals(first, pairedObj.first) &&
Objects.equals(second, pairedObj.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
public static <T, U> Pair<T, U> of(T first, U second) {
return new Pair<>(first, second);
}
public void ifPresent(BiConsumer<? super T, ? super U> consumer) {
if (first != null && second != null) {
consumer.accept(first, second);
}
}
}