fork(1) download
  1. import java.util.Comparator;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4. import java.util.stream.Stream;
  5.  
  6. class Ideone {
  7. public static void main (String[] args) {
  8. final List<B> bs = Stream.<A>of(new B(), new B(), new B(), new A())
  9. .filter(e -> e instanceof B)
  10. .map(e -> (B) e)
  11. .sorted(Comparator.comparingInt(B::getValue))
  12. .collect(Collectors.toList());
  13. System.out.println(bs);
  14. }
  15. }
  16.  
  17. class A {}
  18.  
  19. class B extends A {
  20. private static int globalValue = 5;
  21.  
  22. private int value;
  23.  
  24. public B() {
  25. value = globalValue--;
  26. }
  27.  
  28. public int getValue() {
  29. return value;
  30. }
  31.  
  32. @Override
  33. public String toString() {
  34. return String.format("B={value: %s}", getValue());
  35. }
  36. }
Success #stdin #stdout 0.09s 35004KB
stdin
Standard input is empty
stdout
[B={value: 3}, B={value: 4}, B={value: 5}]