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.function.*;
  7. import java.util.stream.*;
  8. import static java.util.stream.Collectors.*;
  9. import java.util.List;
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. List<T> source = new ArrayList<>();
  16. source.add(new T(12345, 9000, 999));
  17. source.add(new T(12345, 8000, 999));
  18. source.add(new T(1234, 4500, 888));
  19. source.add(new T(1234, 8500, 888));
  20. source.add(new T(1234, 7500, 888));
  21. source.add(new T(1234, 7500, 999));
  22. source.add(new T(1234, 7500, 999));
  23. source.add(new T(1234, 8500, 999));
  24. int offerId = 999;
  25. List<T> result = source.stream()
  26. .filter(x -> x.getOfferId() == offerId)
  27. .collect(toMap(T::getCustomerId,
  28. Function.identity(),
  29. BinaryOperator.maxBy(Comparator.comparingInt(T::getStake))))
  30. .values()
  31. .stream()
  32. .collect(groupingBy(T::getStake))
  33. .values()
  34. .stream()
  35. .flatMap(x -> x.stream().limit(20))
  36. .collect(Collectors.toList());
  37.  
  38.  
  39. System.out.println(result);
  40.  
  41. }
  42. }
  43.  
  44. class T {
  45. public T(int customerId, int stake, int offerId) {
  46. this.customerId = customerId;
  47. this.stake = stake;
  48. this.offerId = offerId;
  49. }
  50.  
  51. @Override
  52. public String toString() {
  53. return "T{" +
  54. "customerId=" + customerId +
  55. ", stake=" + stake +
  56. ", offerId=" + offerId +
  57. '}';
  58. }
  59.  
  60. int customerId;
  61.  
  62. public int getCustomerId() {
  63. return customerId;
  64. }
  65.  
  66. public void setCustomerId(int customerId) {
  67. this.customerId = customerId;
  68. }
  69.  
  70. public int getStake() {
  71. return stake;
  72. }
  73.  
  74. public void setStake(int stake) {
  75. this.stake = stake;
  76. }
  77.  
  78. public int getOfferId() {
  79. return offerId;
  80. }
  81.  
  82. public void setOfferId(int offerId) {
  83. this.offerId = offerId;
  84. }
  85.  
  86. int stake;
  87. int offerId;
  88. }
Success #stdin #stdout 0.15s 2184192KB
stdin
Standard input is empty
stdout
[T{customerId=1234, stake=8500, offerId=999}, T{customerId=12345, stake=9000, offerId=999}]