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

  • Home
  • SEARCH
  • 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 410339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:52:44+00:00 2026-05-12T17:52:44+00:00

I have a class Odp. I want to use TreeSet to keep a sorted

  • 0

I have a class Odp. I want to use TreeSet to keep a sorted collection of Odp objects. However, I’ve been having problems.

public class OdpStorage {

    private TreeSet<Odp> collection = new TreeSet<Odp>(); 

    public addOdp(Odp o) {
          return collection.add(o);
    }

    public int size() {
          return collection.size();
    }

}

collection.add(Odp o) is supposed to do nothing if it’s already in the tree, right? Somehow, this unit test fails:

OdpStorage ts = new OdpStorage();       
Odp ftw = new Odp("LOL");
    Odp ktr = new Odp("OMG");

    ts.addOdp(ftw);

    ts.addOdp(ftw); //should do nothing
    ts.addOdp(ftw); //should do nothing
    ts.addOdp(ftw); //should do nothing
    ts.addOdp(ktr);

assertEquals(2, ts.size());

The assertion fails. It expects 2, but the return value is 5. Why? Could the odp.equals() function be messed up?

Similarly, calling collection.contains(o) fails, even when the there is an object in the set X for which o.equals(X) returns true.

The .equals() function of Odp: (generated by Eclipse)

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Odp))
        return false;
    Gene other = (Odp) obj;
    if (sequence == null) {
        if (other.sequence != null)
            return false;
    } else if (!sequence.equals(other.sequence))
        return false;
    return true;
}

compareTo:

/**
 * this = g0
 * if they are equal, g1 is presumed to come first
 * 
 *  @return -1 if g0 comes before g1; 1 if g0 comes after g1
 */
@Override
public int compareTo(Odp g1) {

    if (sequence.length() < g1.getSeq().length()) {
        return -1;
    }
    else if (sequence.length() > g1.getSeq().length()) {
        return 1;
    }

    if (sequence.compareTo(g1.getSeq()) < 0) {
        return -1;
    }

    return 1;
}

hashCode() is not overridden. Problem?

UPDATE
hashCode() is as follows:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((sequence == null) ? 0 : sequence.hashCode());
    return result;
}

But that still doesn’t solve the problem.

  • 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-12T17:52:44+00:00Added an answer on May 12, 2026 at 5:52 pm

    It appears that your collection.add(o) is failing to find the object in the backing TreeMap. Does your Odp implement Comparable or are you setting a default Comparable on your TreeSet whose compare method you have implemented? If so, you will need to ensure that your compareTo (for the Comparable), or your Comparator compare method will return 0 if the objects passed in are equals.

    EDIT (in response to your comment to the original post):

    It is recommended that you override HashCode() whenever you override equals()

    EDIT2 in response to your compareTo implementation:

    If g0 and g1 are equal, you should return 0. This is the root of the problem.

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

Sidebar

Related Questions

I have class A: public B { ...} vector<A*> v; I want to do
I have class in server side and I want use method of this class
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class A and list of A objects. A has a function f
If I have a class: class Odp { int i; int b; union {
I have a class that looks like: class Foo { public: Foo(); virtual ~Foo();
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
i have class point: public class Point { private double _x; private double _y;
I have class A that derives from System.Data.Objects.DataClasses.EntityObject . When I try to serialize
I have class student : public class Student { public string Name { get;

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.