java treeset<>,java TreeSet:比较和相等

I'd like to have list of object sorted with property 'sort_1'. But when I want to remove I'd like it to use property 'id'. The following code represents the problem.

package javaapplication1;

import java.util.TreeSet;

public class MyObj implements Comparable {

public long sort_1;

public long id;

public MyObj(long sort, long id) {

this.sort_1=sort;

this.id=id;

}

@Override

public int compareTo(MyObj other) {

int ret = Long.compare(sort_1, other.sort_1);

return ret;

}

public String toString() {

return id+":"+sort_1;

}

public static void main(String[] args) {

TreeSet lst=new TreeSet();

MyObj o1 = new MyObj(99,1);

MyObj o2 = new MyObj(11,9);

lst.add(o1);

lst.add(o2);

System.out.println(lst);

MyObj o3 = new MyObj(1234, 1);

//remove myObje with id 1

boolean remove=lst.remove(o3);

System.out.println(lst);

}

}

Output of this code is:

[9:11, 1:99]

[9:11, 1:99]

I need to have list sorted as I do a lot of additions to the list. I don't want to explicitly use any 'sort' method. What are my options ?

EDIT:

My requirement is to have: objects with 'id' as unique but there can be object's with duplicate 'sort' value.

解决方案

I think the problem you're having is that you are implementing Comparable, but your implementation seems to be inconsistent with equals - and you have not implemented any equality methods. That is:

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C

In your case, when you build these three objects:

MyObj o1 = new MyObj(99,1);

MyObj o2 = new MyObj(11,9);

MyObj o3 = new MyObj(1234, 1);

You will see that o1.compareTo(o3) == -1, while o1.equals(o3) == false.

But you seem to want o1.equals(o3) == true.

Also, recognize that TreeSet.add() returns false if the object already exists in the set. This check is based on the equals() method.

To remedy this, override Object.equals() and Object.hashCode() such that they take into consideration the MyObj.id field, and continue to use the sort_1 field in the compareTo() method when they are not equal.

package javaapplication1;

import java.util.TreeSet;

public class MyObj implements Comparable {

public long sort_1;

public long id;

public MyObj(long sort, long id) {

this.sort_1 = sort;

this.id = id;

}

@Override

public int compareTo(MyObj other) {

return (this.equals(other))? 0 : Long.compare(sort_1, other.sort_1);

}

@Override

public boolean equals(Object obj) {

MyObj other = (MyObj) obj;

return this.id == other.id && this.sort_1 == other.sort_1;

}

@Override

public int hashCode() {

return (int) id;

}

public String toString() {

return id + ":" + sort_1;

}

public static void main(String[] args) {

TreeSet lst = new TreeSet();

MyObj o1 = new MyObj(99L, 1L);

MyObj o2 = new MyObj(11L, 9L);

MyObj o3 = new MyObj(1234L, 1L);

MyObj o4 = new MyObj(1234L, 1L);

System.out.println( "Adding o1: " + lst.add(o1));

System.out.println( "Adding o2: " + lst.add(o2));

System.out.println( "Adding o3: " + lst.add(o3));

System.out.println( "Adding o4: " + lst.add(o4));

System.out.println(lst);

System.out.println("o1.compareTo(o3) : " + o1.compareTo(o3));

System.out.println("o1.equals(o3) : " + o1.equals(o3));

//remove myObje with id 1

boolean remove = lst.remove(o3);

System.out.println(lst);

}

}

Output:

Adding o1: true

Adding o2: true

Adding o3: true

Adding o4: false

[9:11, 1:99, 1:1234]

o1.compareTo(o3) : -1

o1.equals(o3) : false

[9:11, 1:99]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值