Search code examples
javatreeset

Look-up equal element in TreeSet


I'm developing a historic view of some elements. Each element has a start and end date. The periods may not overlap, so each start date has to be equal or later then its predecessor's end date. If an end date is null the element is active from its start date until the end date is known.

For testing purposes I have created this class:

public class Entry implements Comparable<Entry>
{
    Integer start;
    Integer end;

    public Entry(Integer s, Integer e)
    {
        start = s;
        end = e;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof Entry)
        {
            return compareTo((Entry) obj) == 0;
        }
        return false;
    }

    @Override
    public int compareTo(Entry o)
    {
        if (o.end != null // other ends before or when this starts
                && (o.end.equals(start) || o.end < start ))
        {
            return 1;
        }
        if (end != null // other starts after or when this ends
                && (o.start.equals(end) || o.start > end ))
        {
            return -1;
        }
        return 0;
    }
}

I use a TreeSet to sort the elements. Now I have the problem that I cannot get the current active or first coming element.

Looking at the JavaDoc the ceiling method should do the trick:

Returns the least element in this set greater than or equal to the given element, or null if there is no such element.

However this doesn't work.

In a test case I create a TreeSet with a bunch of Entries:

TreeSet<Entry> ts = new TreeSet<Entry>();
ts.add(new Entry(1, 3));
ts.add(new Entry(3, 5));
ts.add(new Entry(5, 7));
ts.add(new Entry(7, 9));
ts.add(new Entry(9, 11));
ts.add(new Entry(11, 13));
ts.add(new Entry(13, 15));

Then I get the ceiling using the following code:

ts.ceiling(new Entry(5, null));

The result I expect is the Entry with start 5 and end 7 (the 'equal' Entry). However the result is the Entry with start 7 and end 9 (the greater Entry). Both results qualify as equal to or greater than the given element. But since the JavaDoc mentions it returns the least element, I expect the 5-7 Entry.


Solution

  • You are defining what is to be the least element (by defining the compareTo).

    And you ARE yourself stating that:

    "Both results qualify as equal"

    So when the API refers to the least element, it refers to the element after or equal (ceil) to the argument in the ordered set. Now print your ordered set:

    [(1, 3), (3, 5), (5, 7), (7, 9), (9, 11), (11, 13), (13, 15)]

    So ceiling first looks for the element equal to 5, null (meaning compareTo returns 0) and if it finds one returns that one. You have two elements that are equal (so no need to look for the one after that).

    That is what the ceiling method documentation is refering to, the the order in the set (not by setting up some sort of new comparison).

    public E ceiling(E e)

    Returns the least element in this set greater than or equal to the given element

    See TreeSet#ceiling(E e) API

    So it finds either 5, 7 or 7,9 (which are both equal to 5, null) and returns the one it finds first, which is 7,9 according to the implementation.

    A TreeSet is actually a Tree-structure behind the scenes (doh) and which node in that tree it hits of two equal ones depend on the details of the implementation and possibly order of insertion.

    Your compareTo/equals doesn't obey the normal/recommended rules (certainly bad for a Tree trying to use them). If A and B are equal and C and B are equal then A and C should be equal, that is not the case with your compareTo function.