fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  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) {
  11.  
  12. List<DbTrans> transList = Arrays.asList(
  13. new DbTrans("Troy", "1123", " Savings", 50),
  14. new DbTrans("Larry", "4233", " Savings", 200),
  15. new DbTrans("Troy", "1123", " Current", 120),
  16. new DbTrans("Larry", "4233", " Current", 220)
  17. );
  18.  
  19. List<DbTransResponse> collect = transList.stream()
  20. .collect(Collectors.groupingBy(DbTrans::getAcct))
  21. .values().stream()
  22. .map(r -> new DbTransResponse(
  23. r.get(0).getName(),
  24. r.get(0).getAcct(),
  25. r.get(0).getTotal(),
  26. r.get(1).getTotal()
  27. )).collect(Collectors.toList());
  28.  
  29. collect.forEach(System.out::println);
  30.  
  31. }
  32. }
  33.  
  34. class DbTransResponse {
  35. private String name;
  36. private String acct;
  37. private double totalSavings;
  38. private double totalCurrent;
  39.  
  40. public DbTransResponse(String name, String acct, double totalSavings, double totalCurrent) {
  41. this.name = name;
  42. this.acct = acct;
  43. this.totalSavings = totalSavings;
  44. this.totalCurrent = totalCurrent;
  45. }
  46.  
  47. public String getName() {
  48. return name;
  49. }
  50.  
  51. public void setName(String name) {
  52. this.name = name;
  53. }
  54.  
  55. public String getAcct() {
  56. return acct;
  57. }
  58.  
  59. public void setAcct(String acct) {
  60. this.acct = acct;
  61. }
  62.  
  63. public double getTotalSavings() {
  64. return totalSavings;
  65. }
  66.  
  67. public void setTotalSavings(double totalSavings) {
  68. this.totalSavings = totalSavings;
  69. }
  70.  
  71. public double getTotalCurrent() {
  72. return totalCurrent;
  73. }
  74.  
  75. public void setTotalCurrent(double totalCurrent) {
  76. this.totalCurrent = totalCurrent;
  77. }
  78.  
  79. @Override
  80. public String toString() {
  81. return "DbTransResponse{" +
  82. "name='" + name + '\'' +
  83. ", acct='" + acct + '\'' +
  84. ", totalSavings=" + totalSavings +
  85. ", totalCurrent=" + totalCurrent +
  86. '}';
  87. }
  88. }
  89.  
  90. class DbTrans {
  91. private String name;
  92. private String acct;
  93. private String type;
  94. private double total;
  95.  
  96. public DbTrans(String name, String acct, String type, double total) {
  97. this.name = name;
  98. this.acct = acct;
  99. this.type = type;
  100. this.total = total;
  101. }
  102.  
  103. public String getName() {
  104. return name;
  105. }
  106.  
  107. public void setName(String name) {
  108. this.name = name;
  109. }
  110.  
  111. public String getAcct() {
  112. return acct;
  113. }
  114.  
  115. public void setAcct(String acct) {
  116. this.acct = acct;
  117. }
  118.  
  119. public String getType() {
  120. return type;
  121. }
  122.  
  123. public void setType(String type) {
  124. this.type = type;
  125. }
  126.  
  127. public double getTotal() {
  128. return total;
  129. }
  130.  
  131. public void setTotal(double total) {
  132. this.total = total;
  133. }
  134. }
Success #stdin #stdout 0.14s 2184192KB
stdin
Standard input is empty
stdout
DbTransResponse{name='Larry', acct='4233', totalSavings=200.0, totalCurrent=220.0}
DbTransResponse{name='Troy', acct='1123', totalSavings=50.0, totalCurrent=120.0}