Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:03:09+00:00 2026-06-04T10:03:09+00:00

I’m developing a historic view of some elements. Each element has a start and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:03:12+00:00Added an answer on June 4, 2026 at 10:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.