【问题标题】:SortedSet<TestClass> comparing on equality one field and sorting by anotherSortedSet<TestClass> 比较一个字段是否相等并按另一个字段排序
【发布时间】:2011-11-16 12:13:31
【问题描述】:

请检查代码:

/* Run1.java */
package test;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class Run1 
{
    static public void main(String[] args)
    {
        SortedSet<TestClass> s = new TreeSet<TestClass>(); 

        s.add( new TestClass("name1", 100) );
        s.add( new TestClass("name2", 10) );
        s.add( new TestClass("name3", 1) );
        s.add( new TestClass("name4", 10) );
        s.add( new TestClass("name5", 100) );

        Iterator<TestClass> it = s.iterator();

        while(it.hasNext())
        {
            TestClass t = it.next();
            System.out.println( t.name+' '+t.value );
        }
    }
}

/* TestClass.java */
package test;

public class TestClass implements Comparable<TestClass> 
{
    public String name;
    public int value;

    public TestClass(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public int compareTo(TestClass o) 
    {
        return this.value - o.value;
    }

    public boolean equals(Object o) 
    {
        if (!(o instanceof TestClass))
            return false;
        TestClass n = (TestClass)o;

        return this.name.equals(n.name);
    }

    public int hashCode() 
    {
        return 31*name.hashCode();
    }

    public String toString() 
    {
        return name;
    }
}

打印出来

name3 1
name2 10
name1 100

正如我所见,因为 compareTo 用于检查是否相等(返回 0 时)。但我需要按字段 TestClass.name 检查唯一性,并且仅按 TestClass.value 排序

【问题讨论】:

  • 你的问题没有意义。 TreeSet 使用compareTo() 方法,而不是equals()hashCode() 方法,来确定排序顺序(这是对等式的概括,在排序集的上下文中)。如果这些不是所需的语义,使用不同的数据结构。也就是说:caveat emptor - 有很好的理由说明为什么树集不使用 equals() 来实现对象相等。如果a.compareTo(b)0a.equals(b)false 怎么办?如果a.compareTo(b) 不为零,但a.equals(b)true,该怎么办?
  • 谢谢!请告知适当的数据容器。我需要按键(TestClass.value)快速检索元素,但键可能重复

标签: java treeset sortedset


【解决方案1】:

compareTo()equals()的结果在这种情况下需要兼容,这意味着你需要在比较中考虑相等性。例如:

public int compareTo(TestClass o) 
{
    return (this.value == o.value) ? this.name.compareTo(o.name) : this.value - o.value;
}

它为具有相同值的对象引入了名称子顺序,使结果与您的 equals() 实现兼容。

【讨论】:

    【解决方案2】:

    如何破解 compareTo 方法如下:

    public int compareTo(TestClass o) 
    {
        if (this.name != null && this.name.equals(o.name)) {
            return 0;
        }
    
    
        return this.value - o.value;
    }
    

    这应该在对值进行排序时对名称进行相等检查(删除重复项)

    【讨论】:

      【解决方案3】:

      如果我理解正确,那么您想要的是您的 compareTo 始终为类实现“自然顺序”。这意味着类的客户期望类的行为方式。合同上,compareTo 应该与 equals 一致,这就是为什么我总是将 equals 实现为:

      return compareTo(obj)==0;
      

      这保证了一致性。

      然后,如果您想要另一个排序顺序,您应该实现另一个实现 Comparable 的类。通过这种方式,您可以拥有类一致性和单独的排序顺序。

      【讨论】:

        【解决方案4】:

        编写比较 TestClass 对象的比较器。

        public class TVComparator implements Comparator<TestClass> {
            public int compare(TestClass o1, TestClass o2) {
                if (o1.name.equals(o2.name)) return 0;
                return o1.value - o2.value;
            }
        }
        

        为简单起见,我省略了对空值的任何检查。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-19
          • 2014-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-14
          • 2021-05-02
          相关资源
          最近更新 更多