So I've been struggling with a problem for a while now, figured I might as well ask for help here.
I'm adding Ticket objects to a TreeSet, Ticket implements Comparable and has overridden equals(), hashCode() and CompareTo() methods. I need to check if an object is already in the TreeSet using contains(). Now after adding 2 elements to the set it all checks out fine, yet after adding a third it gets messed up.
running this little piece of code after adding a third element to the TreeSet, Ticket temp2 is the object I'm checking for(verkoopLijst).
Ticket temp2 = new Ticket(boeking, TicketType.STANDAARD, 1,1);
System.out.println(verkoop.getVerkoopLijst().first().hashCode());
System.out.println(temp2.hashCode());
System.out.println(verkoop.getVerkoopLijst().first().equals(temp2));
System.out.println(verkoop.getVerkoopLijst().first().compareTo(temp2));
System.out.println(verkoop.getVerkoopLijst().contains(temp2));
returns this:
22106622
22106622
true
0
false
Now my question would be how this is even possible?
Edit:
public class Ticket implements Comparable{
private int rijNr, stoelNr;
private TicketType ticketType;
private Boeking boeking;
public Ticket(Boeking boeking, TicketType ticketType, int rijNr, int stoelNr){
//setters
}
#Override
public int hashCode(){
return boeking.getBoekingDatum().hashCode();
}
#Override
#SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object o){
Ticket t = (Ticket) o;
if(this.boeking.equals(t.getBoeking())
&&
this.rijNr == t.getRijNr() && this.stoelNr == t.getStoelNr()
&&
this.ticketType.equals(t.getTicketType()))
{
return true;
}
else return false;
}
/*I adjusted compareTo this way because I need to make sure there are no duplicate Tickets in my treeset. Treeset seems to call CompareTo() to check for equality before adding an object to the set, instead of equals().
*/
#Override
public int compareTo(Object o) {
int output = 0;
if (boeking.compareTo(((Ticket) o).getBoeking())==0)
{
if(this.equals(o))
{
return output;
}
else return 1;
}
else output = boeking.compareTo(((Ticket) o).getBoeking());
return output;
}
//Getters & Setters
On compareTo contract
The problem is in your compareTo. Here's an excerpt from the documentation:
Implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.
Your original code is reproduced here for reference:
// original compareTo implementation with bug marked
#Override
public int compareTo(Object o) {
int output = 0;
if (boeking.compareTo(((Ticket) o).getBoeking())==0)
{
if(this.equals(o))
{
return output;
}
else return 1; // BUG!!!! See explanation below!
}
else output = boeking.compareTo(((Ticket) o).getBoeking());
return output;
}
Why is the return 1; a bug? Consider the following scenario:
Given Ticket t1, t2
Given t1.boeking.compareTo(t2.boeking) == 0
Given t1.equals(t2) return false
Now we have both of the following:
t1.compareTo(t2) returns 1
t2.compareTo(t1) returns 1
That last consequence is a violation of the compareTo contract.
Fixing the problem
First and foremost, you should have taken advantage of the fact that Comparable<T> is a parameterizable generic type. That is, instead of:
// original declaration; uses raw type!
public class Ticket implements Comparable
it'd be much more appropriate to instead declare something like this:
// improved declaration! uses parameterized Comparable<T>
public class Ticket implements Comparable<Ticket>
Now we can write our compareTo(Ticket) (no longer compareTo(Object)). There are many ways to rewrite this, but here's a rather simplistic one that works:
#Override public int compareTo(Ticket t) {
int v;
v = this.boeking.compareTo(t.boeking);
if (v != 0) return v;
v = compareInt(this.rijNr, t.rijNr);
if (v != 0) return v;
v = compareInt(this.stoelNr, t.stoelNr);
if (v != 0) return v;
v = compareInt(this.ticketType, t.ticketType);
if (v != 0) return v;
return 0;
}
private static int compareInt(int i1, int i2) {
if (i1 < i2) {
return -1;
} else if (i1 > i2) {
return +1;
} else {
return 0;
}
}
Now we can also define equals(Object) in terms of compareTo(Ticket) instead of the other way around:
#Override public boolean equals(Object o) {
return (o instanceof Ticket) && (this.compareTo((Ticket) o) == 0);
}
Note the structure of the compareTo: it has multiple return statements, but in fact, the flow of logic is quite readable. Note also how the priority of the sorting criteria is explicit, and easily reorderable should you have different priorities in mind.
Related questions
What is a raw type and why shouldn't we use it?
How to sort an array or ArrayList ASC first by x and then by y?
Should a function have only one return statement?
This could happen if your compareTo method isn't consistent. I.e. if a.compareTo(b) > 0, then b.compareTo(a) must be < 0. And if a.compareTo(b) > 0 and b.compareTo(c) > 0, then a.compareTo(c) must be > 0. If those aren't true, TreeSet can get all confused.
Firstly, if you are using a TreeSet, the actual behavior of your hashCode methods won't affect the results. TreeSet does not rely on hashing.
Really we need to see more code; e.g. the actual implementations of the equals and compareTo methods, and the code that instantiates the TreeSet.
However, if I was to guess, it would be that you have overloaded the equals method by declaring it with the signature boolean equals(Ticket other). That would lead to the behavior that you are seeing. To get the required behavior, you must override the method; e.g.
#Override
public boolean equals(Object other) { ...
(It is a good idea to put in the #Override annotation to make it clear that the method overrides a method in the superclass, or implements a method in an interface. If your method isn't actually an override, then you'll get a compilation error ... which would be a good thing.)
EDIT
Based on the code that you have added to the question, the problem is not overload vs override. (As I said, I was only guessing ...)
It is most likely that the compareTo and equals are incorrect. It is still not entirely clear exactly where the bug is because the semantics of both methods depends on the compareTo and equals methods of the Boeking class.
The first if statement of the Ticket.compareTo looks highly suspicious. It looks like the return 1; could cause t1.compareTo(t2) and t2.compareTo(t1) to both return 1 for some tickets t1 and t2 ... and that would definitely be wrong.
Related
Coming from a c++ world, I find reading of the HashSet documentation somewhat hard:
https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
In c++, you would have:
http://en.cppreference.com/w/cpp/container/set
which in turns points to:
http://en.cppreference.com/w/cpp/concept/Compare
Which makes it obvious the requirement for the type of element handled by a std::set. My question is: What are the requirements for the type (E) of elements maintained by a Set in Java ?
Here is a short example which I fail to understand:
import gdcm.Tag;
import java.util.Set;
import java.util.HashSet;
public class TestTag
{
public static void main(String[] args) throws Exception
{
Tag t1 = new Tag(0x8,0x8);
Tag t2 = new Tag(0x8,0x8);
if( t1 == t2 )
throw new Exception("Instances are identical" );
if( !t1.equals(t2) )
throw new Exception("Instances are different" );
if( t1.hashCode() != t2.hashCode() )
throw new Exception("hashCodes are different" );
Set<Tag> s = new HashSet<Tag>();
s.add(t1);
s.add(t2);
if( s.size() != 1 )
throw new Exception("Invalid size: " + s.size() );
}
}
The above simple code fails with:
Exception in thread "main" java.lang.Exception: Invalid size: 2 at TestTag.main(TestTag.java:42)
From my reading of the documentation only the equals operator needs to be implemented for Set:
https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
What am I missing from the documentation ?
I just tried to reproduce your issue, and maybe you just didn't override equals and/or hashSet correctly.
Take a look at my incorrect implemenation of Tag:
public class Tag {
private int x, y;
public Tag(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Tag tag) {
if (x != tag.x) return false;
return y == tag.y;
}
#Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
Looks quite ok doesn't it? But the problem is, I actually do not override the correct equals method, I overloaded it with my own implementation.
To work correctly, equals has to look like this:
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tag tag = (Tag) o;
if (x != tag.x) return false;
return y == tag.y;
}
What am I missing from the documentation ?
You are looking at the wrong part of the documentation.
The C++ set is an "sorted set of unique objects", and are "usually implemented as red-black trees."
In Java, Set is a more abstract concept (it's an interface, not a class) with multiple implementations, most notably the HashSet and the TreeSet (ignoring concurrent implementations).
As you can probably guess from the name alone, the Java TreeSet is the equivalent of the C++ set.
As for requirements, HashSet uses the hashCode() and equals() methods. They are defined on the Object class, and needs to be overridden on classes that needs to be in a HashSet or as keys in a HashMap.
For TreeSet and keys of TreeMap, you have two options: Provide a Comparator when creating the TreeSet (similar to C++), or have the objects implement the Comparable interface.
I guess this was simply a combination of bad luck and misunderstanding of HashSet requirement. Thanks to #christophe for help, I realized the issue when I tried adding in my swig generated Tag.java class:
#Override
public boolean equals(Object o) {
}
I got the following error message:
gdcm/Tag.java:78: error: method does not override or implement a method from a supertype
#Override
^
1 error
1 warning
Which meant my error was simply:
I had the wrong signature in the first place: boolean equals(Object o) != boolean equals(Tag t)
The hint was simply to use the #Override keyword.
For those asking for the upstream code, the Java code is generated by swig. The original c++ code is here:
https://github.com/malaterre/GDCM/blob/master/Source/DataStructureAndEncodingDefinition/gdcmTag.h
I have a class Product, which three variables:
class Product implements Comparable<Product>{
private Type type; // Type is an enum
Set<Attribute> attributes; // Attribute is a regular class
ProductName name; // ProductName is another enum
}
I used Eclipse to automatically generate the equal() and hashcode() methods:
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((attributes == null) ? 0 : attributes.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (attributes == null) {
if (other.attributes != null)
return false;
} else if (!attributes.equals(other.attributes))
return false;
if (type != other.type)
return false;
return true;
}
Now in my application I need to sort a Set of Product, so I need to implement the Comparable interface and compareTo method:
#Override
public int compareTo(Product other){
int diff = type.hashCode() - other.getType().hashCode();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
diff = attributes.hashCode() - other.getAttributes().hashCode();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0;
}
Does this implementation make sense? What about if I just want to sort the product based on the String values of "type" and "attributes" values. So how to implement this?
Edit:
The reason I want to sort a Set of is because I have Junit test which asserts on the string values of a HashSet. My goal is to maintain the same order of output as I sort the set. otherwise, even if the Set's values are the same, the assertion will fail due to random output of a set.
Edit2:
Through the discussion, it's clear that to assert the equality of String values of a HashSet isn't good in unit tests. For my situation I currently write a sort() function to sort the HashSet String values in natural ordering, so it can consistently output the same String value for my unit tests and that suffice for now. Thanks all.
Looks like from all the comments in here you dont need to use Comparator at all. Because:
1) You are using HashSet that does not work with Comparator. It is not ordered.
2) You just need to make sure that two HashSets containing Products are equal. It means they are same size and contain the same set of Products.
Since you already added hashCode and equals methods to Product all you need to do is call equals method on those HashSets.
HashSet<Product> set1 = ...
HashSet<Product> set2 = ...
assertTrue( set1.equals(set2) );
This implementation does not seem to be consistent. You have no control over how the hash codes look like. If you have obj1 < obj2 according to compareTo in the first try, the next time you start your JVM it could be the other way around obj1 > obj2.
The only thing that you really know is that if diff == 0 then the objects are considered to be equal. However you can also just use the equals method for that check.
It is now up to you how you define when obj1 < obj2 or obj1 > obj2. Just make sure that it is consistent.
By the way, you know that the current implementation does not include ProductName name in the equals check? Dont know if that is intended thus the remark.
The question is, what do you know about that attributes? Maybe they implement Comparable (for example if they are Numbers), then you can order according to their compareTo method. If you totally know nothing about the objects, it will be hard to build up a consistent ordering.
If you just want them to be ordered consistently but the ordering itself does not play any role, you could just give them ids at creation time and sort by them. At this point you could indeed use the hashcodes if it does not matter that it can change between JVM calls, but only then.
I'm trying to implement a custom sorted binary tree in java, and I've had some problems with comparisons. I'm trying to implement a generic tree, so I would like to do something like:
public boolean contains(int i){
if(i == currentNode.value)
return true;
else if (i > currentNode.value)
// Go right
else if (i < currentNode.value)
// Go left
...
}
But I'm using objects (and I'm assuming they are objects that can be compared), so I wanted to do something like:
public boolean contains(Object o){
if(o == currentNode.value) // value is of Object class
return true;
else if (o > currentNode.value) // Problem
// Go right
...
}
So my problem right now is that it's not possible to use the operators > and < with objects, and so far I've been unable to think of another way to go about this.
Objects are not in inherently comparable, is an apple greater then an orange? Objects that are comparable implement the interface Comparable. So the simple answer is to replace every instance of Object in your binary tree with Comparable and have
int comparison = o.compareTo(currentNode.Value)
if(comparison ==0){
...
} else if (comparison > 0) {
...
} else {
// comparison < 0
...
}
You probably want to learn about java generics, which make your code much more type safe.
You would start like this
public class BinaryTree<T extends Comparable> {
public boolean contains(T target) {
....
}
...
}
Then you could use it like
BinaryTree<Integer> tree = ...
tree.add(1);
tree.add(2);
tree.add("three"); // <-- Syntax error, compiler would fail.
...
Integer first = tree.getFirst();
Integer last = tree.getLast();
Objects that implement Comparable in java will have an method called compareTo that returns a integer. You just need to do your > and < tests on that, that is:
int res = comparable.compareTo(other);
if(res == 0){
return true;
} else if(res > 0){
//go right
...
} else {
//go left
...
}
If you are using any Object, then override the equals() method, for equality check. If you want to compare them, then implement the Comparable interface, or create a Comparator for them.
This answer won't explain in detail, how to use them, please read the docs (They are pretty detailed, and give you a good start, on the usage), or search for example usages, the internet is full of them.
This is exactly what java comparable and generics are for!
Your objects should implement the: "int compareTo(Object obj)" method. If you were creating a binary search tree your contain method would look something like:
public class MyTree<T extends Comparable<T>>{
...
public boolean contains(TreeNode<T> node, T target)(
if(node == null) return false;
if(node.data.equals(target)){
return true;
}
if(node.data.compareTo(target) > 0){//node is greater than target go left
return contains(node.left, target);
}else{
return contains(node.right, target)//node is less than target go right
}
}
}
I'd compare more than one only field of a object using the compareTo method. Is it possible?
for istance:
public int compareTo(Object o) {
return field.compareTo(o.field);
}
I create this method to sort a collection. Obviously my object has to implement Comparable interface.
I'm guessing if is possible to compare not only one field in the same method compareTo.
Yes, it's possible. If the result of comparing the first field returns zero, then return the result of comparing the second field.
public int compareTo(SomeClass o) {
int result = field1.compareTo(o.field1);
if ( result == 0 ) {
result = field2.compareTo(o.field2);
}
return result;
}
This gets cumbersome fairly quickly, which is why Guava provides a ComparisonChain. Example use:
public int compareTo(SomeClass o) {
return ComparisonChain.start()
.compare(field1, o.field1)
.compare(field2, o.field2)
.result();
}
Yes, it's possible, for example like so:
public int compareTo(MyClass o){
int ret = field1.compareTo(o.field1);
if (ret != 0) return ret;
ret = field2.compareTo(o.field2);
if (ret != 0) return ret;
...
return fieldN.compareTo(o.fieldN);
}
You can certainly factor in other fields when comparing, but there's typically some order of precedence, just like sorting alphabetically only looks at the second letter if the first is the same:
public int compareTo(Object o){
int comparison = field.compareTo(o.field);
if (comparison != 0)
return comparison;
comparison = field2.compareTo(o.field2);
if (comparison != 0)
return comparison;
//etc...
}
sure you can.
however you must define the rule of comparison .
e.g.
you have
objectA{a=1;b=2;c=3}
objectB{a=20;b=1;c=6}
in your compareTo(Object o) method, you could compare this.fields with o.fields. you can even compare this.a to o.c if you really need. point is you have to define the rule, in which case objectA < objectB. etc..
Sure. Here's a relatively concise way of doing it.
public int compareTo(MyClass other) {
return
a!=other.a ? Integer.compare(a, other.a) :
b!=other.b ? Integer.compare(b, other.b) :
Integer.compare(c, other.c);
}
(Integer.compare is from Java SE 7, but the implementation isn't difficult. Assumes int fields a, b, c, but is essentially the same for any field types you can compare.)
public int compareTo(Object o){
int res = field.compareTo(o.field);
if(res==0)
res=field1.compareTo(o.field1);
return res;
}
should work
You can do it any of following ways:
public int compareTo(Object o)
{
return (field.compareTo(o.field)==1 && field2.compareTo(o.field2)==0)? 0 : 1;
}
OR
public int compareTo(Object o)
{
// add various if-else blocks
// OR
// call a separate method
}
here is my compareTo method, but im still getting "missing return statement" warning.
can anyone tell me what is wrong with my code?
public int compareTo(Flows other) {
if(this.srcAddr.equals(other.srcAddr)){
if(this.dstAddr.equals(other.dstAddr)){
if(this.srcPort.equals(other.srcPort)){
if(this.dstPort.equals(other.dstPort)){
if(this.protocol.equals(other.protocol)){
return 0;
}
}
}
}
}
}
Two things:
You get the "missing return statement" because there are paths of execution where no value is returned. For example, when the first if statement computes to false.
You are breaking the compareTo() contract. For the following call: a.compareTo(b), the result should be: 0 if a equals b, <0 if a is minor than b, and >0 if a is greater than b. It seems you're using the compareTo() to check for equality, in that case the correct approach is overriding the equals() method.
This looks like an equals method. If the intention simply is to compare if the two are the same, I would do something like
return srcAddr.equals(other.srcAddr) &&
dstAddr.equals(other.dstAddr) &&
srcPort.equals(other.srcPort) &&
dstPort.equals(other.dstPort) &&
protocol.equals(other.protocol);
If it's not the intention, you're probably breaking the contract of compareTo since your method doesn't seem to adhere to the transitivity requirement. From the docs of Comparable:
The implementor must also ensure that the relation is transitive
It's because there's a possibility in your code for the compareTo to return nothing! Think about if all of those if statements fail, then it will hit the end of the method and not have returned anything. You need a return further down:
public int compareTo(Flows other) {
if(this.srcAddr.equals(other.srcAddr)){
if(this.dstAddr.equals(other.dstAddr)){
if(this.srcPort.equals(other.srcPort)){
if(this.dstPort.equals(other.dstPort)){
if(this.protocol.equals(other.protocol)){
return 0;
}
}
}
}
}
return 1;
}
Also you are not doing a complete compare. You need to return 0 if they are equal, less than 0 if the difference is less than and greater than 0 if it's greater. It seesm you'd be better off with overriding equals!
Maybe something like:
public boolean equals(Flows other) {
return (this.srcAddr.equals(other.srcAddr) && this.dstAddr.equals(other.dstAddr) && this.srcPort.equals(other.srcPort) && this.dstPort.equals(other.dstPort) && this.protocol.equals(other.protocol));
just add a "return 1" (or anything) at the end of the function, and it should solve the issue.
This will compile and run, but what about the rest of the contract? Where's less than and greater than?
public int compareTo(Flows other) {
int value = 0;
if(this.srcAddr.equals(other.srcAddr)){
if(this.dstAddr.equals(other.dstAddr)){
if(this.srcPort.equals(other.srcPort)){
if(this.dstPort.equals(other.dstPort)){
if(this.protocol.equals(other.protocol)){
value = 0;
}
}
}
}
return value;
}