I have read numerous posts about TreeSets, Comparable/Comparator Interfaces, equals, compareTo, compare methods and I know that API says you have to make your ordering "consistent with equals" or weird things might happen.
But in my case and I think this is a fairly general case, I really do need a TreeSet ordering which is "inconsistent with equals".
Lets say we are doing some kind of heuristic search and we are expanding (or generating) new states starting from a root(initial) state. And we put new (expanded/generated) states into a TreeSet which we usually call open list. We would like to use the TreeSet container because we dont want duplicate states in our open list.
Every state generated/expanded is evaluated by a cost function and given a heuristic value which shows the quality of the state. We want the TreeSet (open list) ordered by this value. We want to have the best states (which has the best cost value) at the top of the TreeSet.
Now here comes the problem. To accommodate the ordering by cost value we need to give the TreeSet a comparator which compares the cost values. But, two different states can have the same cost/heuristic value. I want to have both of these states on my open list because they are not "equal". But comparator needs to return 0 from the compare method because they have the same cost value. And because this, different states with the same cost value will not be inserted into the list.
I want to give a simple example to make this more understandable. Lets say our states are strings showing binary data and the cost function counts the number of "1"s in the string.
Lets say these are the generated states and their respective cost values.
No State Cost
1 01001001 3
2 01101001 4
3 10001001 3
4 01001111 5
As you can see all these 4 states are different. They are "not equal". But even though state-1 and state-3 are different, they have the same cost value "3". So when we order our TreeSet by cost, state-3 will not be added to the TreeSet because there is already an element with the same cost value. But we need that state to be added to the list because it is perfectly valid, different, new state.
How can I overcome this problem?
Thanks.