Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8309367

TreeSet can add duplicate item

    XMLWordPrintable

Details

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Window 10
      Java 17, Java 20

      A DESCRIPTION OF THE PROBLEM :
      Add two student objects same id to a TreeSet

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Step 1: I have a student class with structure: Integer id, String classCode, String fullName, double mark
      Step 2: Init a student list have 5 students
      Step 3: Create a TreeSet with Comparator compare id
      Step 4: Add all 5 students to TreeSet
      Step 5: Edit student 1 -> Change id
      Step 6: [BUG] Add student one more time

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result: Don't add student 1 again to Set
      ACTUAL -
      Actually result: Set have 2 student with same id

      ---------- BEGIN SOURCE ----------
      public class Student {

          private Integer id;
          private String fullName;
          private String classCode;
          private double mark;

          public Student(Integer id, String fullName, String classCode) {
              this.id = id;
              this.fullName = fullName;
              this.classCode = classCode;
          }
          public Student(Integer id, String fullName, String classCode, double mark) {
              this.id = id;
              this.fullName = fullName;
              this.classCode = classCode;
              this.mark = mark;
          }

          public Integer getId() {
              return id;
          }

          public void setId(Integer id) {
              this.id = id;
          }

          public String getFullName() {
              return fullName;
          }

          public void setFullName(String fullName) {
              this.fullName = fullName;
          }

          public String getClassCode() {
              return classCode;
          }

          public void setClassCode(String classCode) {
              this.classCode = classCode;
          }

          public double getMark() {
              return mark;
          }

          public void setMark(double mark) {
              this.mark = mark;
          }

          @Override
          public String toString() {
              return "Student{" +
                      "id=" + id +
                      ", fullName='" + fullName + '\'' +
                      ", classCode='" + classCode + '\'' +
                      ", mark=" + mark +
                      '}';
          }
      }

      public class TreeSetDemoTest {

          public static void main(String[] args) {

              TreeSet<Student> tSet = new TreeSet<>((s1, s2) -> Integer.compare(s1.getId(), s2.getId()));
              Student st1 = new Student(1, "Nguyen Van A", "FJB01", 6);
              Student st2 = new Student(2, "Nguyen Van B", "FJB01", 8);
              Student st3 = new Student(2, "Nguyen Van C", "FJB02", 4);
              Student st4 = new Student(4, "Nguyen Van D", "FJB02", 10);
              Student st5 = new Student(5, "Nguyen Van E", "FJB03", 8);

              System.out.println(tSet.addAll(List.of(st1, st2, st3, st4, st5)));
              st1.setId(69);
              tSet.add(st1);
              tSet.forEach(System.out::println);

              for (Student student : tSet) {
                  System.out.println(student == st1);
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: