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

TreeSet cannot be deduplicated, if the element is added more than twice

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      windows10

      A DESCRIPTION OF THE PROBLEM :
      In java 1.8.0_241 version, treeSet cannot be deduplicated. If the set element exceeds 2, it cannot be deduplicated. It will be possible later. Here is the code

      REGRESSION : Last worked in version 8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      @Data
      public class AreaMapEntity implements Comparable<AreaMapEntity> {

          // 国家 代码
          private String countrySimpleCode;
          // 国家英文
          private String countryEnglishName;
          // 国家总和
          private long countryHotNumber = 0L;

          // 国家下面的城市
          private Set<CityMapEntity> citys = new HashSet<>();

          public static class CityMapEntity implements Comparable<CityMapEntity> {
              private String citySimpleCode;
              private String cityEnglishName;
              private long cityUserHotNumber = 0L;

              public CityMapEntity() {
              }

              public CityMapEntity(String citySimpleCode, String cityEnglishName, long cityUserHotNumber) {
                  this.citySimpleCode = citySimpleCode;
                  this.cityEnglishName = cityEnglishName;
                  this.cityUserHotNumber = cityUserHotNumber;
              }

              @Override
              public boolean equals(Object o) {
                  if (this == o) return true;
                  if (o == null || getClass() != o.getClass()) return false;
                  CityMapEntity that = (CityMapEntity) o;
                  return Objects.equals(cityEnglishName, that.cityEnglishName);
              }

              @Override
              public int hashCode() {
                  return Objects.hash(cityEnglishName);
              }

              @Override
              public int compareTo(CityMapEntity o) {
                  if (this.getCityEnglishName().equals(o.getCityEnglishName())) {
                      return 0;
                  } else {
                      if (this.getCityUserHotNumber() <= o.getCityUserHotNumber()) {
                          return 1;
                      } else {
                          return -1;
                      }
                  }

                  /*if (this.getCityUserHotNumber() <= o.getCityUserHotNumber()) {
                      return 1;
                  } else {
                      return -1;
                  }*/
              }

              public String getCitySimpleCode() {
                  return citySimpleCode;
              }

              public void setCitySimpleCode(String citySimpleCode) {
                  this.citySimpleCode = citySimpleCode;
              }

              public String getCityEnglishName() {
                  return cityEnglishName;
              }

              public void setCityEnglishName(String cityEnglishName) {
                  this.cityEnglishName = cityEnglishName;
              }

              public long getCityUserHotNumber() {
                  return cityUserHotNumber;
              }

              public void setCityUserHotNumber(long cityUserHotNumber) {
                  this.cityUserHotNumber = cityUserHotNumber;
              }
          }


          @Override
          public int compareTo(AreaMapEntity areaMapEntity) {
              if (this.getCountrySimpleCode().equals(areaMapEntity.getCountrySimpleCode())) {
                  return 0;
              } else {
                  if (this.getCountryHotNumber() <= areaMapEntity.getCountryHotNumber()) {
                      return 1;
                  } else {
                      return -1;
                  }
              }
          }


          @Override
          public boolean equals(Object o) {
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
              AreaMapEntity that = (AreaMapEntity) o;
              return Objects.equals(countrySimpleCode, that.countrySimpleCode);
          }

          @Override
          public int hashCode() {
              return Objects.hash(countrySimpleCode);
          }
      }
      public static void main(String[] args) throws Exception {
              Set<AreaMapEntity> res = new TreeSet<>();

              AreaMapEntity a = new AreaMapEntity();
              a.setCountrySimpleCode("MM");
              a.setCountryEnglishName("Myanmar");

              AreaMapEntity b = new AreaMapEntity();
              b.setCountrySimpleCode("MY");
              b.setCountryEnglishName("Malaysia");

              AreaMapEntity c = new AreaMapEntity();
              c.setCountrySimpleCode("TH");
              c.setCountryEnglishName("Thailand");

              AreaMapEntity d = new AreaMapEntity();
              d.setCountrySimpleCode("SA");
              d.setCountryEnglishName("Saudi Arabia");


              AreaMapEntity e = new AreaMapEntity();
              e.setCountrySimpleCode("MM");
              e.setCountryEnglishName("Myanmar");


              res.add(a);
              res.add(a);
              res.add(a);
              res.add(b);
              res.add(c);
              res.add(d);
              res.add(e);

              System.out.println(res);
          }

      ACTUAL -
      The expected result is that it can be deduplicated but cannot be deduplicated in practice. The variable e is saved in, and there is no need to save the variable e.

      ---------- BEGIN SOURCE ----------
      @Data
      public class AreaMapEntity implements Comparable<AreaMapEntity> {

          // 国家 代码
          private String countrySimpleCode;
          // 国家英文
          private String countryEnglishName;
          // 国家总和
          private long countryHotNumber = 0L;

          // 国家下面的城市
          private Set<CityMapEntity> citys = new HashSet<>();

          public static class CityMapEntity implements Comparable<CityMapEntity> {
              private String citySimpleCode;
              private String cityEnglishName;
              private long cityUserHotNumber = 0L;

              public CityMapEntity() {
              }

              public CityMapEntity(String citySimpleCode, String cityEnglishName, long cityUserHotNumber) {
                  this.citySimpleCode = citySimpleCode;
                  this.cityEnglishName = cityEnglishName;
                  this.cityUserHotNumber = cityUserHotNumber;
              }

              @Override
              public boolean equals(Object o) {
                  if (this == o) return true;
                  if (o == null || getClass() != o.getClass()) return false;
                  CityMapEntity that = (CityMapEntity) o;
                  return Objects.equals(cityEnglishName, that.cityEnglishName);
              }

              @Override
              public int hashCode() {
                  return Objects.hash(cityEnglishName);
              }

              @Override
              public int compareTo(CityMapEntity o) {
                  if (this.getCityEnglishName().equals(o.getCityEnglishName())) {
                      return 0;
                  } else {
                      if (this.getCityUserHotNumber() <= o.getCityUserHotNumber()) {
                          return 1;
                      } else {
                          return -1;
                      }
                  }

                  /*if (this.getCityUserHotNumber() <= o.getCityUserHotNumber()) {
                      return 1;
                  } else {
                      return -1;
                  }*/
              }

              public String getCitySimpleCode() {
                  return citySimpleCode;
              }

              public void setCitySimpleCode(String citySimpleCode) {
                  this.citySimpleCode = citySimpleCode;
              }

              public String getCityEnglishName() {
                  return cityEnglishName;
              }

              public void setCityEnglishName(String cityEnglishName) {
                  this.cityEnglishName = cityEnglishName;
              }

              public long getCityUserHotNumber() {
                  return cityUserHotNumber;
              }

              public void setCityUserHotNumber(long cityUserHotNumber) {
                  this.cityUserHotNumber = cityUserHotNumber;
              }
          }


          @Override
          public int compareTo(AreaMapEntity areaMapEntity) {
              if (this.getCountrySimpleCode().equals(areaMapEntity.getCountrySimpleCode())) {
                  return 0;
              } else {
                  if (this.getCountryHotNumber() <= areaMapEntity.getCountryHotNumber()) {
                      return 1;
                  } else {
                      return -1;
                  }
              }
          }


          @Override
          public boolean equals(Object o) {
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
              AreaMapEntity that = (AreaMapEntity) o;
              return Objects.equals(countrySimpleCode, that.countrySimpleCode);
          }

          @Override
          public int hashCode() {
              return Objects.hash(countrySimpleCode);
          }
      }
      public static void main(String[] args) throws Exception {
              Set<AreaMapEntity> res = new TreeSet<>();

              AreaMapEntity a = new AreaMapEntity();
              a.setCountrySimpleCode("MM");
              a.setCountryEnglishName("Myanmar");

              AreaMapEntity b = new AreaMapEntity();
              b.setCountrySimpleCode("MY");
              b.setCountryEnglishName("Malaysia");

              AreaMapEntity c = new AreaMapEntity();
              c.setCountrySimpleCode("TH");
              c.setCountryEnglishName("Thailand");

              AreaMapEntity d = new AreaMapEntity();
              d.setCountrySimpleCode("SA");
              d.setCountryEnglishName("Saudi Arabia");


              AreaMapEntity e = new AreaMapEntity();
              e.setCountrySimpleCode("MM");
              e.setCountryEnglishName("Myanmar");


              res.add(a);
              res.add(a);
              res.add(a);
              res.add(b);
              res.add(c);
              res.add(d);
              res.add(e);

              System.out.println(res);
          }
      ---------- END SOURCE ----------

      FREQUENCY : always


            ccleary Conor Cleary (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: