Advertisement
DulcetAirman

above/below average

Feb 23rd, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package ch.fhnw.claudemartin;
  2.  
  3. import java.util.*;
  4. import java.util.concurrent.atomic.LongAdder;
  5.  
  6. public class SomeClass {
  7.   public static void main(final String[] args) {
  8.     final double[] data = { -12.7, -8.3, 2, 2.4, 3.5, 5.8, 21.3 };
  9.  
  10.     final DoubleSummaryStatistics stats = Arrays.stream(data)
  11.         .summaryStatistics();
  12.     final double avg = stats.getAverage();
  13.     final long count = stats.getCount();
  14.     System.out.println("average: " + avg);
  15.  
  16.     final Map<String, LongAdder> map = new TreeMap<>();
  17.     final LongAdder below = map.compute("below", (k, v) -> new LongAdder());
  18.     final LongAdder above = map.compute("above", (k, v) -> new LongAdder());
  19.     final LongAdder equal = map.compute("equal", (k, v) -> new LongAdder());
  20.  
  21.     Arrays.stream(data).forEach(d -> (d > avg ? above : d < avg ? below : equal).increment());
  22.  
  23.     map.entrySet().stream().forEach(e -> System.out.format("%s: %f %%%n",
  24.         e.getKey(), 100D * e.getValue().longValue() / count));
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement