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

The Archive Base Latest Questions

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

IF we add two different objects(mutable) in HashSet and after that change the value

  • 0

IF we add two different objects(mutable) in HashSet and after that change the value of the objects by calling setter(making them same), the size remains 2 of the hashSet

I am not able to understand the reason for the same :

public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("Amit");
        Employee e1 = new Employee();
        e1.setName("Jitender");
        Set<Person> hashSet = new HashSet<Person>();
        hashSet.add(e);
        hashSet.add(e1);
        // size of set is >>2
        System.out.println("size of set is >>" + hashSet.size());
        e1.setName("Amit");
        // updated size of set is >>2
        System.out.println("updated size of set is >>" + hashSet.size());
}

Employee class is :

public class Employee extends Person {

    public Employee() {
    }

    public Employee(String name) {
        this.name = name;
    }

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return name.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        Employee e = (Employee) obj;
        return this.name.equals(e.name);
    }
}
  • 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:05:02+00:00Added an answer on June 3, 2026 at 6:05 am

    HashSet is not intended to react to changes in the objects it contains. In fact, the documentation warns against adding mutable objects to a set and modifying them.

    From the Set interface documentation:

    Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

    ( Another point is that HashSet uses the members’ equals() and hashCode() methods, which means that if you did not implement those methods for your Employee, two Employees will not be equal unless they are two references to the same Employee object. ) In your edit you show that you implemented that.

    On a practical “what happens if I do this anyway?” note, if you look at the source code of HashMap (b/c a HashSet is implemented as a HashMap that maps the entries you insert to a dummy object) you can see that the size is only updated when you add and remove variables, and that even when the table is rehashed to increase the capacity, there are no equality checks, so your duplicate objects will stay in that set that you just broke by doing what you shouldn’t have been doing in the first place.

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

Sidebar

Related Questions

I'm trying to add two BitSet objects together (mutating one of them). This should
I want to add two lists of a numeric type such that addedList[x] =
How can I add two label in the same grid box? e.g. in row
How can one add two long values in Java so that if the result
Summary: I want to save two classes of the same name and different namespaces
I'd like to add different types of objects derived from one class with generics
I want to compare two objects of different versions and display their differences in
I'm trying to use HashSet to store objects of a class that I created,
What is the difference between the following two statements? alter table [dbo].[Demo] add constraint
I'm trying to add two inputs to a QTCaptureSession in the following: mainSession =

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.