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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:06:30+00:00 2026-06-03T06:06:30+00:00

I’ve been using ArrayList for my project to store a cricket team players and

  • 0

I’ve been using ArrayList for my project to store a cricket team players and order them.
I started thinking about using a TreeSet because of its advantage of removing duplicates.
However the problem I’m having is that if for example I create the following two players:

P p1 = new P("Jack","Daniel",33(age),180(height),78(weight),41(games played),2300
(runs scored),41(dismisses))
P p2 = new P("Jack","Daniel",37(age),185(height),79(weight),45(games played),2560
(runs scored),45(dismisses))

Notice that the two players have the same first and last name, but everything else is different. When I try to add these two players to the TreeSet, it considers them duplicates because of the names similarities and removes the second one. Obviously I don’t want this to happen and I want the Set to remove a player only if everything he has is the same as another player, and not just the first and last names.

Is there a way of achieving this?

Also my TreeSet takes a Player object.

  • 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-03T06:06:31+00:00Added an answer on June 3, 2026 at 6:06 am

    Originally, this answer neglected the fact that a TreeSet does its comparisons based on compareTo(), rather than equals(). Edits have been made to address this.

    You need to define equals(), hashCode() and compareTo() for your Player object correctly. (Since it’s a TreeSet and not a HashSet, implementing hashCode() isn’t so important – but it’s good practice.)

    Equals and hashCode need to take into account all of the fields. Eclipse can auto-generate one for you that will look similar to this (Source > Generate hashcode and equals).

    If you already have a natural sort order that doesn’t use all of the fields, then you could supply a custom comparator to your TreeSet. However, even if you really only want to sort by a subset of the fields, there’s nothing stopping you sorting by all fields (with the uninteresting fields only playing a part of the interesting parts are identical). The important thing to note here is that a TreeSet determines equality not by the equals() method, but by compareTo() == 0.

    Here’s an example equals():

    @Override
    public boolean equals(Object obj)
    {
      if (this == obj) {
        return true;
      }
      if (obj == null) {
        return false;
      }
      if (getClass() != obj.getClass()) {
        return false;
      }
    
      Player that = (Player) obj;
      return this.age == that.age &&
             this.height == that.height &&
             this.weight == that.weight &&
             this.games == that.games &&
             this.runs == that.runs &&
             this.dismisses == that.dismisses &&
             this.given.equals(that.given) &&
             this.family.equals(that.family);
    }
    

    And here’s hashcode:

    @Override
    public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + this.age;
      result = prime * result + this.dismisses;
      result = prime * result + this.family.hashCode());
      result = prime * result + this.games;
      result = prime * result + this.given.hashCode());
      result = prime * result + this.height;
      result = prime * result + this.runs;
      result = prime * result + this.weight;
      return result;
    }
    

    Finally, here’s a compareTo:

    public int compareTo(Player that)
    {
      int result;
    
      result = this.family.compareTo(that.family); 
      if (result != 0)                              // is the family name different?
      {
        return result;                              // yes ... use it to discriminate
      }
    
      result = this.given.compareTo(that.given);
      if (result != 0)                              // is the given name different?
      {
        return result;                              // yes ... use it to discriminate
      }
    
      result = this.age - that.age;                 // is the age different?
      if (result != 0)
      {
        return result;                              // yes ... use it to discriminate
      }
    
      ... (and so on) ...
      ... with the final one ...
    
      return this.dismisses - that.dismisses;       // only thing left to discriminate by
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

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.