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?
}
}
问题:如果我想previuosUsers
在调用新的 setUsers 后仍然保留旧值,我应该如何修改这个程序?