fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.ArrayList;
  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) throws java.lang.Exception
  11. {
  12. List<DetailedData> data = getList();
  13.  
  14. List<DetailedData> temp = data.stream()
  15. .collect(Collectors.groupingBy(DetailedData::getModel))
  16. .values().stream()
  17. .map(DetailedData::merge)
  18. .collect(Collectors.toList());
  19.  
  20. temp.forEach(System.out::println);
  21. }
  22.  
  23. public static List<DetailedData> getList() {
  24. List<DetailedData> ret = new ArrayList<>();
  25.  
  26. ret.add(new DetailedData("dell", "inspiron 15-R", "laptop", "Header", "9j4AAQSk", null));
  27. ret.add(new DetailedData("dell", "inspiron 15-R", "laptop", "Thumbnail", "iVBORw0KGg", null));
  28. ret.add(new DetailedData("apple", "macbook air", "laptop", "Header", "9j4AAQSk", null));
  29. ret.add(new DetailedData("apple", "macbook air", "laptop", "Thumbnail", "iVBORw0KGg", null));
  30. ret.add(new DetailedData("dell", "xps 13", "laptop", "Header", "9j4AAQSk", null));
  31.  
  32. return ret;
  33. }
  34. }
  35.  
  36. class DetailedData {
  37. String Company;
  38. String Model;
  39. String Category;
  40. String ImageType;
  41. String ImageBuffer1;
  42. String ImageBuffer2;
  43.  
  44. public DetailedData(String company, String model, String category, String imageType, String imageBuffer1,
  45. String imageBuffer2) {
  46. super();
  47. Company = company;
  48. Model = model;
  49. Category = category;
  50. ImageType = imageType;
  51. ImageBuffer1 = imageBuffer1;
  52. ImageBuffer2 = imageBuffer2;
  53. }
  54.  
  55. public static DetailedData merge(List<DetailedData> list) {
  56. if(list.isEmpty()) // Not possible after 'groupingBy'.
  57. return null;
  58.  
  59. if(list.size() == 1)
  60. return list.get(0);
  61.  
  62. // At least 2 elements:
  63. DetailedData ret = list.get(0);
  64. ret.ImageBuffer2 = list.get(1).ImageBuffer1;
  65. return ret;
  66. }
  67.  
  68. public String getModel() {
  69. return Model;
  70. }
  71.  
  72. public String getImageType() {
  73. return ImageType;
  74. }
  75.  
  76. @Override
  77. public String toString() {
  78. return String.join(", ", Company, Model, Category, ImageType, ImageBuffer1, ImageBuffer2);
  79. }
  80. }
Success #stdin #stdout 0.21s 320960KB
stdin
Standard input is empty
stdout
dell, xps 13, laptop, Header, 9j4AAQSk, null
dell, inspiron 15-R, laptop, Header, 9j4AAQSk, iVBORw0KGg
apple, macbook air, laptop, Header, 9j4AAQSk, iVBORw0KGg