Reputation: 62185
I'm implementing a web application and on the serverside I need to keep track of logged in users. I've implemented a simple Authentication class for this, with methods for logging in/out.
public class AuthenticationServiceImpl extends RemoteServiceServlet implements AuthenticationService {
private final List<User> currentlyLoggedIn = new ArrayList<User>();
@Override
public User login(String username, String password) {
// retrieve user from DB if exists
// add the user to the list/set/whatever
}
@Override
public void logout(User user) {
// remove the user from the datastructure
}
}
Now, I wonder which datastructure would be best to use? First I was thinking of a List
, but as the order doesn't matter, and I need fast add/remove capabilities, I'm now thinking that a HashMap
, using the username
as the key and the User
object as the value would be a better option. Any ideas/suggestions?
Upvotes: 0
Views: 2242
Reputation: 2599
as the order doesn't matter, and I need fast add/remove capabilities
You've got it, an HashMap would be the perfect choice, in my opinion. Just with the fact that you can access a user object by its name, I think this is the way to go.
Upvotes: 1