fork download
  1. import java.util.*;
  2. import java.util.stream.*;
  3.  
  4. class Ideone {
  5. public static void main(String[] args) {
  6. final List<StudentCourseMapping> studentCourseMappings = List.of(
  7. new StudentCourseMapping("Alu", "Physics", 12, "Quantum Theory"),
  8. new StudentCourseMapping("Alu", "Physics", 12, "English"),
  9. new StudentCourseMapping("Sam", "Commerce", 16, "English"),
  10. new StudentCourseMapping("Sam", "Commerce", 16, "Accounts"),
  11. new StudentCourseMapping("Joe", "Arts", 19, "English"),
  12. new StudentCourseMapping("Joe", "Arts", 19, "Hindi"));
  13. final List<StudentCourseMapping> deduped = studentCourseMappings.stream()
  14. .distinct()
  15. .collect(Collectors.toList());
  16. System.out.println(deduped
  17. .stream()
  18. .map(Object::toString)
  19. .collect(Collectors.joining(System.lineSeparator())));
  20. }
  21. }
  22.  
  23. class StudentCourseMapping {
  24. private final String name;
  25. private final String dept;
  26. private final Integer roll;
  27. private final String course;
  28.  
  29. public StudentCourseMapping(String name, String dept, Integer roll, String course) {
  30. this.name = name;
  31. this.dept = dept;
  32. this.roll = roll;
  33. this.course = course;
  34. }
  35.  
  36. public String getName() {
  37. return name;
  38. }
  39.  
  40. public String getDept() {
  41. return dept;
  42. }
  43.  
  44. public Integer getRoll() {
  45. return roll;
  46. }
  47.  
  48. public String getCourse() {
  49. return course;
  50. }
  51.  
  52. @Override
  53. public boolean equals(Object obj) {
  54. StudentCourseMapping other = (StudentCourseMapping) obj;
  55. if (roll == null) {
  56. if (other.roll != null)
  57. return false;
  58. } else if (!roll.equals(other.roll))
  59. return false;
  60. return true;
  61. }
  62.  
  63. @Override
  64. public int hashCode() {
  65. return Objects.hash(roll);
  66. }
  67.  
  68. @Override
  69. public String toString() {
  70. return "StudentCourseMapping{" +
  71. "name='" + name + '\'' +
  72. ", dept='" + dept + '\'' +
  73. ", roll=" + roll +
  74. ", course='" + course + '\'' +
  75. '}';
  76. }
  77. }
Success #stdin #stdout 0.18s 56788KB
stdin
Standard input is empty
stdout
StudentCourseMapping{name='Alu', dept='Physics', roll=12, course='Quantum Theory'}
StudentCourseMapping{name='Sam', dept='Commerce', roll=16, course='English'}
StudentCourseMapping{name='Joe', dept='Arts', roll=19, course='English'}