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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:06:02+00:00 2026-06-07T17:06:02+00:00

I have one class Human , which contains two fields, age(int) , and name(String)

  • 0

I have one class Human, which contains two fields, age(int), and name(String). With eclipse, I override the hashCode() and equals() method with these two fields. I also create a Comparator based on the age field.

Now, I create a TreeSet object with the Comparator of age, and also two instances (with different fields values) of Human class. Then I add these two objects into the set, however, there are always only one object in the set.

For understanding the problem, I print out the hash value of these two objects, and find them are different. Then, I test their equals() method, it does output false when I compare two instances with different field values. So now, I cannot figure out why the TreeSet can’t handle (differentiate) the problem. Can anyone give me some help ? Thanks a lot !

  • 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-07T17:06:03+00:00Added an answer on June 7, 2026 at 5:06 pm

    TreeSet doesn’t use hashCode() and equals() at all. It uses the comparator you pass as argument (or the compareTo() method of the objects if they are Comparable and you don’t provide a comparator). Two objects are considered equal by a TreeSet if compare() (orcompareTo()) returns 0 when comparing these two objects.

    So if your comparator only compares the age of humans, all the humans with the same age will be considered equal. If you want humans to be considered equal when they have the same age and name, then the comparator should compare by age, and then compare by name if the age comparison returns 0.

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

Sidebar

Related Questions

I have one class. Class First { private Second second; public First(int num, String
we have one class which has one float[] and one int[] . Sometimes the
I have one class in which i have two method _addView() and _removeView(). And
Suppose I have one class Person ……… public class Person { public string Name
I have one class called Global and two other activities. In each activity I
I have one parent class, which is abstract class for now, for four different
I have one class with two important functions: public class Foo { //plenty of
I have one class called MainMenu, which has a method called switchViews, with 2
I'm making a program on the HUFFMAN ALGORITHM where I have one class of
I can't figure out why I'm getting this error. I only have one class

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.