fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. double sFF = 0;
  13. double cAT = 0;
  14.  
  15. for (int i = 0; i <= 100; ++i) {
  16. List<Integer> list = new Random(0).ints().limit(5).boxed().collect(Collectors.toList());
  17. long start = System.nanoTime();
  18. list.stream().collect(Collectors.collectingAndThen(
  19. Collectors.maxBy(Comparator.naturalOrder()),
  20. (Optional<Integer> n) -> n.orElse(0)));
  21. long end = System.nanoTime();
  22. cAT = (end - start) / 1e6;
  23.  
  24. start = System.nanoTime();
  25. list.stream().sorted().findFirst().get();
  26. end = System.nanoTime();
  27. sFF = (end - start) / 1e6;
  28.  
  29. if (i == 0 || i == 100) {
  30. System.out.println("Iteration " + i);
  31. System.out.println("collectAndThen " + cAT);
  32. System.out.println("sortFindFirst " + sFF);
  33. }
  34. }
  35. }
  36. }
Success #stdin #stdout 0.23s 34828KB
stdin
Standard input is empty
stdout
Iteration 0
collectAndThen 2.884806
sortFindFirst 1.898522
Iteration 100
collectAndThen 0.00792
sortFindFirst 0.010873