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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:17:32+00:00 2026-06-13T04:17:32+00:00

1.Initially created two sets. 2.Added elements to one set. 3.Assigned one set to another.

  • 0

1.Initially created two sets.
2.Added elements to one set.
3.Assigned one set to another.
4.If clear is called on one set,both the sets are getting cleared.

Can anyone help in figuring out the problem?

import java.util.HashSet;
import java.util.Set;

public class SetOperation {

Set<Integer> a = new HashSet<Integer>();
Set<Integer> b = new HashSet<Integer>();

void assignAndClear(){

    a.add(3);
    a.add(7);
    a.add(5);
    a.add(19);
    a.add(99);

    System.out.println("a:" +a );
    System.out.println("b:" +b );

    b=a;

    System.out.println("After assigning");
    System.out.println("a:" +a );
    System.out.println("b:" +b );

    b.clear();

    System.out.println("after clearing");
    System.out.println("a:" +a );
    System.out.println("b:" +b );       
}
public static void main(String[] args) {

    SetOperation sd = new SetOperation();
    sd.assignAndClear();
}
}
  • 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-13T04:17:33+00:00Added an answer on June 13, 2026 at 4:17 am

    When you assign one set to another, new set is not created, rather a new reference is created that points to the existing set. So, whatever change you make to the set using a will get reflected in b.

    This is true for any Mutable Object.

    But not for Immutable Objects.

    For E.g, Consider the case for String: –

    String s = "a";
    String s1 = s; // Both `s` and `s1` points to `"a"`
    
    s1 = "b";  // Only s1 will point to "b". `s` still points to "a".
    

    In the above case, the change is not reflected to all the reference, because Strings are immutable. So any change you make to String will create a new object.

    But, if you have mutable object.: –

    Set<String> set = new HashSet<String>();
    Set<String> set3 = new HashSet<String>(); // A different set object
    Set<String> set2 = set;   // Point to the same set object as "set"
    
    set2.clear();  // Will clear the actual object. Pointed by "set"
    // And thus both the reference pointing to that object, will see the change.
    
    set3 = set; // Change reference `set3` to point to the `Set object` pointed by `set`
    
    set3.add("a");  // Will change `set` also.
    

    If you want to create a copy of your Set. Do it like this: –

    Set<String> set1 = new HashSet<String>();
    Set<String> set3 = new HashSet<String>(set1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a simple window with two buttons, the first one calls a function
I created a grammar for a Scheme-like language interpreter. Initially, I had one semantic
I have two activities, the one Main.java that initially loads I have a couple
We have few components like libraries dlls When initially created I ran the following
What objects are created initially by compilers(?) of javascript? I've been learning Io in
If a VS2008 project is created initially with a web app project, and class
I need to decrypt a conection that was created initially under an account that
As I've come to understand using $('.whatever').click() only works for items created initially. Additional
I am using MVC4's WEB API to expose a controller. Initially I created created
I've just created a new Virtual Machine, which was initially completely clean - I've

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.