Java HashSet删除重复项,但TreeSet不删除?

Java HashSet删除重复项,但TreeSet不删除?,java,hashset,treeset,Java,Hashset,Treeset,以下类的输出为: 尺码是3号 大小是1 但是如果我将树集更改为哈希集,那么行: Set<SuggestionDetailBean> set = new TreeSet<SuggestionDetailBean>(); Set Set=new TreeSet(); 变成 Set<SuggestionDetailBean> set = new HashSet<SuggestionDetailBean>(); Set Set=newhashset(

以下类的输出为: 尺码是3号 大小是1

但是如果我将树集更改为哈希集,那么行:

Set<SuggestionDetailBean> set = new TreeSet<SuggestionDetailBean>();
Set Set=new TreeSet();
变成

Set<SuggestionDetailBean> set = new HashSet<SuggestionDetailBean>();
Set Set=newhashset();
输出为: 尺码是3号 大小是2

使用HashSet或TreeSet时是否不更改集合的大小? 使用HashSet的行为似乎和预期的一样,因为它正在删除重复项,但是当我使用TreeSet时,重复项仍然存在? 我认为SuggestionDetailBean中的hashcode和equals方法被正确重写了吗

代码如下:

public class TestSet {

    public static void main(String args[]){

        SuggestionDetailBean s = new SuggestionDetailBean();
        s.setTagList("teddst");
        s.setUrl("testurl");

        SuggestionDetailBean s2 = new SuggestionDetailBean();
        s2.setTagList("teddst");
        s2.setUrl("testurl");

        SuggestionDetailBean s3 = new SuggestionDetailBean();
        s3.setTagList("tessdafat");
        s3.setUrl("fdfaasdfredtestur ldd");

        List<SuggestionDetailBean> list = new ArrayList<SuggestionDetailBean>();
        list.add(s);
        list.add(s2);
        list.add(s3);

        Set<SuggestionDetailBean> set = new TreeSet<SuggestionDetailBean>();
        set.addAll(list);

        System.out.println("size is "+list.size());
        System.out.println("size is "+set.size());

    }

}

public class SuggestionDetailBean implements Comparable<Object> {

    private String url;
    private String tagList;
    private String numberOfRecommendations;
    private String date;
    private String time;
    private String summary;
    private String truncatedUrl;


    public void setTruncatedUrl(String truncatedUrl) {

        if(truncatedUrl.length() > 20){
            truncatedUrl = truncatedUrl.substring(0, 20)+"...";
        }

        this.truncatedUrl = truncatedUrl;
    }

    public String getSummary() {
        if(summary == null){
            return "";
        }
        else {
            return summary;
        }
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }


        public String getTime() {
            return time;
        }

        public String getTruncatedUrl() {
            return this.truncatedUrl;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getTagList() {
            if(tagList == null){
                return "";
            }
            else {
                return tagList;
            }
        }

        public void setTagList(String tagList) {
            this.tagList = tagList;
        }


        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getNumberOfRecommendations() {
            return numberOfRecommendations;
        }

        public void setNumberOfRecommendations(String numberOfRecommendations) {
            this.numberOfRecommendations = numberOfRecommendations;
        }

        @Override
        public int compareTo(Object o) {

            DateFormat formatter;
            Date date1 = null;
            Date date2 = null;  
            SuggestionDetailBean other = (SuggestionDetailBean) o;

            if(this.date == null || other.date == null){
                return 0;
            }   
            formatter = new SimpleDateFormat(SimpleDateFormatEnum.DATE.getSdfType()+" "+SimpleDateFormatEnum.TIME.getSdfType());
            try {
                date1 = (Date) formatter.parse(this.date + " " + this.time);
                date2 = (Date) formatter.parse(other.date + " " + other.time);
            } catch (ParseException e) {
                System.out.println("Exception thrown in"+this.getClass().getName()+", compareTo method");
                e.printStackTrace();
            }
            catch(NullPointerException npe){
                System.out.println("Exception thrown "+npe.getMessage()+" date1 is "+date1+" date2 is "+date2);
            }

             return date2.compareTo(date1);

        }

        @Override
           public int hashCode() {
                return this.url.hashCode();
            }

        @Override
        public boolean equals(Object obj) {

            SuggestionDetailBean suggestionDetailBean = (SuggestionDetailBean) obj;

            if(StringUtils.isEmpty(this.getTagList())){
                return this.getUrl().equals(suggestionDetailBean.getUrl());
            }
            else {
                return (this.getTagList().equals(suggestionDetailBean.getTagList())) &&
                        (this.getUrl().equals(suggestionDetailBean.getUrl()));
            }

        }

    }
公共类测试集{
公共静态void main(字符串参数[]){
SuggestionDetailBean s=新建SuggestionDetailBean();
s、 设置列表(“teddst”);
s、 setUrl(“testurl”);
SuggestionDetailBean s2=新的SuggestionDetailBean();
s2.设定值列表(“teddst”);
s2.setUrl(“testurl”);
SuggestionDetailBean s3=新建SuggestionDetailBean();
s3.设置列表(“Tessafat”);
s3.setUrl(“fdfaasdfredtestur ldd”);
列表=新的ArrayList();
列表。添加(s);
列表。添加(s2);
增加(s3);
Set=新树集();
set.addAll(列表);
System.out.println(“大小为”+list.size());
System.out.println(“大小为”+set.size());
}
}
公共类SuggestionDetailBean实现了可比较的{
私有字符串url;
私有字符串标记列表;
私人建议;
私有字符串日期;
私有字符串时间;
私有字符串摘要;
私有字符串截断;
公共void setTruncatedUrl(字符串truncatedUrl){
如果(truncatedUrl.length()>20){
truncatedUrl=truncatedUrl.子串(0,20)+“…”;
}
this.truncatedUrl=truncatedUrl;
}
公共字符串getSummary(){
if(summary==null){
返回“”;
}
否则{
返回摘要;
}
}
公共void集合摘要(字符串摘要){
this.summary=摘要;
}
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共字符串getTime(){
返回时间;
}
公共字符串getRuncateDurl(){
返回此.truncatedUrl;
}
公共无效设置时间(字符串时间){
这个时间=时间;
}
公共字符串getTagList(){
如果(标记列表==null){
返回“”;
}
否则{
返回标记列表;
}
}
public void setTagList(字符串标记列表){
this.tagList=tagList;
}
公共字符串getUrl(){
返回url;
}
公共void setUrl(字符串url){
this.url=url;
}
公共字符串getNumberOfRecommensions(){
返回建议;
}
public void setNumberOfRecommensions(字符串NumberOfRecommensions){
this.numberOfRecommensions=numberOfRecommensions;
}
@凌驾
公共整数比较对象(对象o){
数据格式格式化程序;
Date date1=null;
日期2=空;
SuggestionDetailBean其他=(SuggestionDetailBean)o;
if(this.date==null | | other.date==null){
返回0;
}   
格式化程序=新的SimpleDataFormat(SimpleDataFormatEnum.DATE.getSdfType()+“”+SimpleDataFormatEnum.TIME.getSdfType());
试一试{
date1=(Date)formatter.parse(this.Date+“”+this.time);
date2=(Date)格式化程序.parse(other.Date+“”+other.time);
}捕获(解析异常){
System.out.println(“在“+this.getClass().getName()+”,compareTo方法”中引发的异常);
e、 printStackTrace();
}
捕获(NullPointerException npe){
System.out.println(“抛出异常”+npe.getMessage()+“date1是”+date1+“date2是”+date2);
}
返回日期2.与(日期1)相比;
}
@凌驾
公共int hashCode(){
返回此.url.hashCode();
}
@凌驾
公共布尔等于(对象obj){
SuggestionDetailBean SuggestionDetailBean=(SuggestionDetailBean)obj;
if(StringUtils.isEmpty(this.getTagList())){
返回此.getUrl().equals(suggestionDetailBean.getUrl());
}
否则{
return(this.getTagList().equals(suggestionDetailBean.getTagList()))&&
(this.getUrl().equals(suggestionDetailBean.getUrl());
}
}
}
编辑: 注意:如果我使用以下命令将哈希集转换为树集:

 Set<SuggestionDetailBean> sortedSet = new TreeSet<SuggestionDetailBean>(hashset);
Set sortedSet=新树集(hashset);
然后保持正确的排序,因为删除重复项是基于对象哈希代码和equals方法,而不是compareto方法。

根据:

请注意,由集合维护的顺序(无论是否为显式 如果提供比较器,则必须与equals一致 正确执行
设置
界面。(请参见可比的 或
比较器
,以获得与 这是因为
Set
接口是在中定义的
的项等于
操作,但它是一个
树集
实例 使用其
比较器执行所有元素比较
比较
)方法,因此此方法认为相等的两个元素 从集合的角度来看,它们是相等的。集合的行为 定义良好,即使其顺序与equals不一致;信息技术 未能遵守
集合
接口的总合同

因此,问题在于您的
比较方法
            if(this.date == null || other.date == null){
                return 0;
            }