-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPair.java
More file actions
38 lines (31 loc) · 847 Bytes
/
Pair.java
File metadata and controls
38 lines (31 loc) · 847 Bytes
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 Pair;
import java.util.Collections;
/**
* You need to store two values of type `E`, set them in a constructor, and have the following methods,
* getFirst
* getSecond
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair<E extends Comparable> {
private E first;
private E second;
public Pair(E first, E second) {
this.first = first;
this.second = second;
}
public E getFirst() { return first; }
public E getSecond() { return second; }
public <E extends Comparable<? super E>> E min() {
if (first.compareTo(second) == 1) {
return (E) second;
}
return (E) first;
}
public E max() {
if(first.compareTo(second) == -1) {
return second;
}
return first;
}
}