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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:50:21+00:00 2026-06-14T04:50:21+00:00

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class A { private Set<String> users =

  • 0
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class A
{
    private Set<String> users = new HashSet<String>();

    public Set<String> getUsers()
    {
        return users;
    }

    public void setUsers( Set<String> users )
    {
        this.users.clear();
        if( users != null )
        {
            this.users.addAll( users );
        }
    }

    public static void main( String[] args )
    {
        A a = new A();
        Set<String> temp = new HashSet<String>();
        temp.add( "a1" );
        temp.add( "a2" );
        a.setUsers( temp );
        Set<String> previousUsers = a.getUsers();
        System.out.println( previousUsers.size() ); //output 2

        temp.add( "a3" );
        a.setUsers( temp );
        System.out.println( previousUsers.size() );//output 3, how to make it still 2?
    }
}

Question: if i want previuosUsers still keep the old values after new setUsers been called, how should i modify this program?

  • 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-14T04:50:22+00:00Added an answer on June 14, 2026 at 4:50 am

    You can use this constructor instead public HashSet(Collection<? extends E> c), than just directly assigning the return to previousUsers.

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

Sidebar

Related Questions

Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
import java.util.ArrayList; import java.util.Random; public class Generator{ //9352141NTBG1223 public static void main(String[] args) {
import java.util.ArrayList; public class WTFAMIDOINGWRONG { public static void main(String[] args) { ArrayList<Integer> intsAR
Hi have one class like this import java.util.ArrayList; public class MobilePhone { private String
import java.util.ArrayList; public class ListOfClasses { private ArrayList<Class> classes; public ArrayList<Class> getClasses() { return
import java.util.ArrayList; import java.util.Arrays; public class Cloud { private ArrayList<Point> points; private double left;
import java.util.ArrayList; import java.lang.Math; public class War { ArrayList deck = new ArrayList(0); ArrayList
package vaannila; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String
Here is the whole code : import java.util.ArrayList; public class Test<E extends Comparable<E>>{ ThreadLocal<ArrayList<E>>arraylist=new
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private

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.