tan8888 2013-03-11 12:31
浏览 368
已采纳

treeSet 一个很诡异的重复性判断问题

有一个需求是这样的,要求去掉重复Id的文章,如果id相同就去掉,如果不同就按文章发表时间排序,但写完代码发现,id相同的文章怎么也去重不了:

[code="java"]
public class ArticleSource implements Comparable {

private long id;
private int sourceId;
private long time;

public ArticleSource(long id,int sourceId,long time) {
    this.id = id;
    this.sourceId = sourceId;
    this.time = time;;
}

public long getId() {
    return id;
}

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

public int getSourceId() {
    return sourceId;
}


public void setSourceId(int sourceId) {
    this.sourceId = sourceId;
}

public long getTime() {
    return time;
}


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

@Override
public boolean equals(Object obj) {
    System.out.println("equals");
    if(obj instanceof ArticleSource) {
        ArticleSource at = (ArticleSource)obj;
        if(at.getId() == this.getId()) {
            return true;
        }
    }

    return false;
}

@Override
public int hashCode() {
    System.out.println("hash");
     return (int) id;
}

@Override
public int compareTo(ArticleSource o) {

    if(o == null) {
        return 0;
    }
    System.out.println(o.getId() + "=>"+this.getId() );
    if(o.getId() == this.getId()) {
        return 0;
    }

    if(o.getTime() >this.getTime()) {
        return -1;
    }else if(o.getTime()==this.getTime()){
        return 0;
    }else {
        return 1;
    }

}

@Override
public String toString() {
    return id +":" + time;
}



public static void main(String[] args) {
    Set<ArticleSource> set = new TreeSet<ArticleSource>();

    set.add(new ArticleSource(391454L, 1420, 13626506898983L));
    set.add(new ArticleSource(3914064L, 1420, 10000L));
    set.add(new ArticleSource(3914065L, 4235, 1362650633576L));
    set.add(new ArticleSource(3914064L, 4235, 1363333333333L));

    System.out.println(set);

}

[/code]

结果:
[code="java"]
391454=>3914064
391454=>3914065
3914064=>3914065
3914065=>3914064
391454=>3914064
[3914064:10000, 3914065:1362650633576, 3914064:1363333333333, 391454:13626506898983]

[/code]

诡异的是,第二个和第四个id是相同的,竟然无法去重,更诡异的是,把第四个的time 少一位,就是136333333333L,又可以去掉重复,请教一下,是什么原因?难道long不允许超过13位吗
结果:
[code="java"]
391454=>3914064
391454=>3914065
3914064=>3914065
3914065=>3914064
3914064=>3914064
[3914064:10000, 3914065:1362650633576, 391454:13626506898983]
[/code]

  • 写回答

8条回答 默认 最新

  • liangjie5305579 2013-03-11 17:23
    关注

    将compareTo方法改成
    [code="java"]public int compareTo(Object o1) {
    ArticleSource o = (ArticleSource)o1;
    int result = this.getId() > o.getId() ? 1 : (this.getId() == o.getId() ? 0 : -1);
    if (result == 0) {
    result = this.getId() > o.getId() ? 1 : (this.getId() == o.getId() ? 0 : -1);
    }
    return result;
    }[/code]
    其实你写的那个compareTo方法是有问题,举个很简单的例子,假如你main里面只有这三条数据
    [code="java"]set.add(new ArticleSource(2L, 1000, 10l));
    set.add(new ArticleSource(1L, 1000, 5l));
    set.add(new ArticleSource(1L, 1000, 30l));[/code]
    你会发现相同id的也没去掉
    当执行第二行的时候去compareTo方法,那么变成了
    [code="java"]1:5
    2:10
    1:30[/code]
    执行第三行的时候“1:30”只与“2:10”对比,未与“1:5”对比

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况