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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:59:46+00:00 2026-06-10T15:59:46+00:00

The doc about java.util.Set.contains(Object o) says: Returns true if and only if this set

  • 0

The doc about java.util.Set.contains(Object o) says:

Returns true if and only if this set contains an element e such that
(o==null ? e==null : o.equals(e)).

That said, here is a POJO (as you can see, I overwrote its equals method):

public class MonthAndDay {

    private int month;
    private int day;

    public MonthAndDay(int month, int day) {
        this.month = month;
        this.day = day;
    }

    @Override
    public boolean equals(Object obj) {
        MonthAndDay monthAndDay = (MonthAndDay) obj;
        return monthAndDay.month == month && monthAndDay.day == day;
    }

}

So please, why does the following code prints false instead of true?

Set<MonthAndDay> set = new HashSet<MonthAndDay>();
set.add(new MonthAndDay(5, 1));
System.out.println(set.contains(new MonthAndDay(5, 1)));
// prints false

A solution is to rewrite the contains(Object o) method, but the original one should be (almost) exactly the same, am I wrong?

Set<MonthAndDay> set = new HashSet<MonthAndDay>() {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean contains(Object obj) {
        MonthAndDay monthAndDay = (MonthAndDay) obj;
        for (MonthAndDay mad : this) {
            if (mad.equals(monthAndDay)) {
                return true;
            }
        }
        return false;
    }

};
set.add(new MonthAndDay(5, 1));
System.out.println(set.contains(new MonthAndDay(5, 1)));
// prints true
  • 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-10T15:59:47+00:00Added an answer on June 10, 2026 at 3:59 pm

    When you override equals(Object) you also need to override hashcode().

    Specifically, the methods must be implemented so that if a.equals(b) is true, then a.hashcode() == b.hashcode() is all true. If this invariant is not respected, then HashMap, HashSet and Hashtable will not work properly.

    The technical details of how hashcode() and equals(Object) should behave are specified in the Object API.


    So why do the hash-based data structures break if you get this wrong? Well basically because a hash table works by using the value of the hash function to narrow down the set of values to be compared with the “candidate”. If the hashcode for the candidate is different to the hashcode for some object in the table, then the chances are that the lookup algorithm won’t compare with the object in the table … even if the objects are equal.

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

Sidebar

Related Questions

in this page of tutorial : http://java.sun.com/javaee/6/docs/tutorial/doc/bnadu.html discuss about hello2 application on samples folder.
From the doc of Java about Hashtable class, it says As a general rule,
I'm doing a chrome extension, and there is in the doc this statement about
I've been reading about this on StackOverflow and facebook doc, yet I dont seem
I have used Java, C++, .Net. (in that order). When asked about by-value vs.
I'm bit confused with Java doc about Matcher in the definition of start() and
I started reading more about ThreadPoolExecutor from Java Doc as I am using it
I am confused about using expm1 function in java The Oracle java doc for
I'm trying to read the cocos2d api doc but I'm confused about what the
Context (doc count) ------------------------- clojure.core/count ([coll]) Returns the number of items in the collection.

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.