Please review code:
/* 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;
}
}
Print out
name3 1
name2 10
name1 100
as i see because compareTo used for checking to equality (when returned 0). But i need check for unique by field TestClass.name and only sort by TestClass.value
The result of
compareTo()andequals()need to be compatible in this case, which means that you need to take into account equality in the comparison. For example:which introduces a sub-order on name for objects with the same value, making the result compatible with your
equals()implementation.