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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:58:54+00:00 2026-06-07T03:58:54+00:00

I have the following. HashSet<String> set1 = new HashSet<String>(); HashSet<String> set2 = new HashSet<String>();

  • 0

I have the following.

HashSet<String> set1 = new HashSet<String>();
HashSet<String> set2 = new HashSet<String>();
String strB1 = "B";
String strB2 = "B";

set1.add( "A" );
set1.add( strB1 );
set2.add( strB2 );

set1.removeAll( set2 ); 

Will set1 end up containing only “A”, or will it contain “B” as well?

  • 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-07T03:58:55+00:00Added an answer on June 7, 2026 at 3:58 am

    HashSet uses object equality (Object.equals), not identity (“reference equals”).

    Additionally, HashSet uses Object.hashCode for the hashing function.

    Unfortunately, to “know” this, takes a little bit of “reading into” of the documentation and knowing how a hash is implemented. From the documentation for HashSet.contains:

    Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).

    The more general Set documentation says:

    A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

    With very few exceptions, such as IdentityHashMap, the data-structures in Java work on equality and not identity.

    Thus, to answer the question, HashSet works on the “String values”.

    (This example can be particularly misleading due to String intern’ing, but that doesn’t change the above as identity implies equality even if the converse is not true.)

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

Sidebar

Related Questions

I have the following code: private static HashSet<SoloUser> soloUsers = new HashSet<SoloUser>(); public void
If I have code that looks like this: Collection<Object> c = new HashSet<Object>(); will
I am new to Hibernate and have the following situation: class A class Course
I am complete new to Hibernate and I have a question in the following
I have the following code private void LoadIntoMemory() { //Init large HashSet HashSet<document> hsAllDocuments
I have the following code: public class MyElement { String name; String type; MyElement(String
I have the following example: package cage; import java.util.HashSet; import java.util.Set; import animals.Animal; public
Please clarify my doubt in Hashset. Consider the following code, class Person { String
I have the following problem: I have a hashset of Pairs. Pair is a
Suppose I have the following User struct: struct User { string userId; UserType userType;

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.