SHARE
TWEET

max of vectors (2)

DulcetAirman Aug 16th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Optional;
  5. import java.util.stream.Stream;
  6.  
  7. public class SomeClass {
  8.   static class Tuple implements Comparable<Tuple> {
  9.     final int max;
  10.     final int[] vector;
  11.  
  12.     public Tuple(int[] vector) {
  13.       assert vector.length > 0;
  14.       this.max = Arrays.stream(vector).max().getAsInt();
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top