-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTupleExamples.java
More file actions
56 lines (51 loc) · 1.85 KB
/
TupleExamples.java
File metadata and controls
56 lines (51 loc) · 1.85 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
50
51
52
53
54
55
56
import java.util.List;
import java.util.ArrayList;
public class TupleExamples {
public static void main(String[] args) {
int[] arr = { 8, 6, 7, 5, 3, 0, 9 };
Tuple2<Integer, Integer> maxAndMin = getLargestAndSmallest(arr);
System.out.println("Max: " + maxAndMin.getFirst());
System.out.println("Min: " + maxAndMin.getSecond());
List<String> list1 = new ArrayList<>();
list1.add("alpha");
list1.add("beta");
list1.add("gamma");
List<String> list2 = new ArrayList<>();
list2.add("gamma");
list2.add("delta");
Tuple2<Integer, String> commonElem = getCommonElement(list1, list2);
if (commonElem.getFirst() == null) {
System.out.println("Lists do not intersect.");
} else {
int index = commonElem.getFirst();
String elem = commonElem.getSecond();
System.out.println("Found common element [" + elem + "] at index " + index);
}
}
public static Tuple2<Integer, Integer> getLargestAndSmallest(int[] arr) {
if (arr == null) {
return null;
} else if (arr.length == 0) {
return new Tuple2<>(null, null);
} else {
int smallest = arr[0];
int largest = arr[0];
for (int i : arr) {
if (smallest > i) {
smallest = i;
} else if (largest < i) {
largest = i;
}
}
return new Tuple2<>(largest, smallest);
}
}
public static <E> Tuple2<Integer, E> getCommonElement(List<E> list1, List<E> list2) {
for (int i = 0; i < list1.size(); i++) {
if (list2.contains(list1.get(i))) {
return new Tuple2<>(i, list1.get(i));
}
}
return new Tuple2<>(null, null);
}
}