JAVA8--List.stream().collect

分手后的思念是犯贱 2023-02-21 12:45 86阅读 0赞
  1. package mypack;
  2. import java.text.ParseException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.stream.Collectors;
  7. public class Test
  8. {
  9. public static void main(String[] args) throws ParseException
  10. {
  11. Student s1 = new Student("1", "S1", 10);
  12. Student s2 = new Student("1", "S1", 10);
  13. Student s3 = new Student("1", "S1", 10);
  14. Student s4 = new Student("2", "S2", 20);
  15. Student s5 = new Student("2", "S2", 20);
  16. Student s6 = new Student("2", "S2", 20);
  17. Student s7 = new Student("3", "S3", 30);
  18. Student s8 = new Student("3", "S3", 30);
  19. Student s9 = new Student("4", "S4", 40);
  20. Student s10 = new Student("4", "S4", 40);
  21. List<Student> sList = new ArrayList<Student>();
  22. sList.add(s1);
  23. sList.add(s2);
  24. sList.add(s3);
  25. sList.add(s4);
  26. sList.add(s5);
  27. sList.add(s6);
  28. sList.add(s7);
  29. sList.add(s8);
  30. sList.add(s9);
  31. sList.add(s10);
  32. Map<ParentStudent, List<Student>> sMap = sList.stream().collect(Collectors.groupingBy(student -> new ParentStudent(student.getId())));
  33. sMap.forEach((key, value) -> {
  34. System.out.print("id:" + key.getId() + "====");
  35. for(Student s : value)
  36. {
  37. System.out.print("name:" + s.getName() + ",");
  38. }
  39. System.out.println("");
  40. });
  41. }
  42. }
  43. class ParentStudent extends SupperStudent
  44. {
  45. String id;
  46. public ParentStudent(String sid)
  47. {
  48. this.id = sid;
  49. }
  50. @Override
  51. public int hashCode()
  52. {
  53. final int prime = 31;
  54. int result = 1;
  55. result = prime * result + ((id == null) ? 0 : id.hashCode());
  56. return result;
  57. }
  58. @Override
  59. public boolean equals(Object obj)
  60. {
  61. if(this == obj)
  62. return true;
  63. if(obj == null)
  64. return false;
  65. if(getClass() != obj.getClass())
  66. return false;
  67. ParentStudent other = (ParentStudent)obj;
  68. if(id == null)
  69. {
  70. if(other.id != null)
  71. return false;
  72. }
  73. else if(!id.equals(other.id))
  74. return false;
  75. return true;
  76. }
  77. public String getId()
  78. {
  79. return id;
  80. }
  81. public void setId(String id)
  82. {
  83. this.id = id;
  84. }
  85. }
  86. class SupperStudent
  87. {
  88. private String sex;
  89. public String getSex()
  90. {
  91. return sex;
  92. }
  93. public void setSex(String sex)
  94. {
  95. this.sex = sex;
  96. }
  97. }
  98. class Student
  99. {
  100. String id;
  101. String name;
  102. int age;
  103. public Student(String sid)
  104. {
  105. this.id = sid;
  106. }
  107. public Student(String sid, String sname, int sage)
  108. {
  109. this.id = sid;
  110. this.name = sname;
  111. this.age = sage;
  112. }
  113. public String getId()
  114. {
  115. return id;
  116. }
  117. public void setId(String id)
  118. {
  119. this.id = id;
  120. }
  121. public String getName()
  122. {
  123. return name;
  124. }
  125. public void setName(String name)
  126. {
  127. this.name = name;
  128. }
  129. public int getAge()
  130. {
  131. return age;
  132. }
  133. public void setAge(int age)
  134. {
  135. this.age = age;
  136. }
  137. }

发表评论

表情:
评论列表 (有 0 条评论,86人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java 8

    事的时候 自己就看看     [跟上 Java 8 – 你忽略了的新特性][Java 8 _] [Java 8 _]: http://www.importnew...

    相关 groovy java8_从Groovy到Java 8

    Groovy开发人员早已熟知Java 8中新引入的概念和新的语言结构了。在Java新版本即将推出的增强特性中,有很多是Groovy在几年前就已经提供了的。从用于函数式编程风格的

    相关 Java8

    [Java8][] 1、 HashMap 加入了红黑树 2、ConcurrentHashMap  使用了CAS无锁机制   3、 永久区没有了,成了元空间(MetaS