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 983379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:50:52+00:00 2026-05-16T04:50:52+00:00

So I’ve been struggling with a problem for a while now, figured I might

  • 0

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
  • 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-05-16T04:50:53+00:00Added an answer on May 16, 2026 at 4:50 am

    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?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.