fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.*;
  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. List<B> list = Arrays.asList(
  13. new B(5, "toto"),new B(6, "toto"),new B(5, "tata"),new B(6, "tata"));
  14. B first5 = getLastBeFromInt(list, 5);
  15. System.out.println(first5);
  16.  
  17. B first6 = getLastBeFromInt(list, 6);
  18. System.out.println(first6);
  19. }
  20.  
  21. static B getLastBeFromInt(List<B> l, int i){
  22. return l.stream().filter(b->b.x==i).reduce((first, second) -> second).orElse(null);
  23. }
  24. }
  25.  
  26.  
  27. class B {
  28. int x;
  29. String y;
  30. public B(int x, String y){this.x=x;this.y=y;}
  31. public String toString(){return x+" "+y;}
  32. }
Success #stdin #stdout 0.2s 33804KB
stdin
Standard input is empty
stdout
5 tata
6 tata