SHOW:
|
|
- or go back to the newest paste.
| 1 | - | package ch.claude_martin.foo; |
| 1 | + | package ch.claude_martin; |
| 2 | ||
| 3 | - | import java.util.*; |
| 3 | + | import java.util.Arrays; |
| 4 | import java.util.Optional; | |
| 5 | import java.util.stream.Stream; | |
| 6 | ||
| 7 | - | public static void main(String[] args) {
|
| 7 | + | |
| 8 | - | final int[] v1 = { 3, 5 };
|
| 8 | + | static class Tuple implements Comparable<Tuple> {
|
| 9 | - | final int[] v2 = { 3, 6 };
|
| 9 | + | final int max; |
| 10 | - | final OptionalInt max = Stream.of(v1, v2).flatMapToInt(Arrays::stream).max(); |
| 10 | + | final int[] vector; |
| 11 | - | if (max.isPresent()) |
| 11 | + | |
| 12 | - | System.out.println(max.getAsInt()); |
| 12 | + | public Tuple(int[] vector) {
|
| 13 | - | else |
| 13 | + | assert vector.length > 0; |
| 14 | - | System.out.println("No data");
|
| 14 | + | this.max = Arrays.stream(vector).max().getAsInt(); |
| 15 | - | } |
| 15 | + | this.vector = vector; |
| 16 | } | |
| 17 | ||
| 18 | @Override | |
| 19 | public int compareTo(Tuple o) {
| |
| 20 | return Integer.compare(o.max, this.max); | |
| 21 | } | |
| 22 | ||
| 23 | public int getMax() {
| |
| 24 | return max; | |
| 25 | } | |
| 26 | ||
| 27 | public int[] getVector() {
| |
| 28 | return vector; | |
| 29 | } | |
| 30 | ||
| 31 | } | |
| 32 | ||
| 33 | public static void main(String[] args) {
| |
| 34 | final int[] v1 = { 3, 5 };
| |
| 35 | final int[] v2 = { 3, 6 };
| |
| 36 | final int[] v3 = {};
| |
| 37 | final int[] v4 = { -1, Integer.MIN_VALUE };
| |
| 38 | final Optional<Tuple> tuple = Stream.of(v1, v2, v3, v4).filter(v -> v.length > 0).map(v -> new Tuple(v)).sorted() | |
| 39 | .findFirst(); | |
| 40 | ||
| 41 | if (tuple.isPresent()) | |
| 42 | System.out.println(tuple.get().getMax() + " -> " + Arrays.toString(tuple.get().getVector())); | |
| 43 | else | |
| 44 | System.out.println("No data");
| |
| 45 | } | |
| 46 | } |