Reputation: 31
I'm experimenting with a small usermanagement-system. I have stored all data with this code:
public static HashMap<String, List<String>> loginData = new HashMap<String, List<String>>();
The key is a username and the values (in the List) are password, first name, last name, id.
How can I check that entered username and password comply with the data in my HashMap?
This is how I checked the username:
if (loginData.containsKey(loginname) == true){
Upvotes: 2
Views: 234
Reputation: 43
boolean validateLogin(String username, String password) {
List<String> userDetails = loginData.get(username);
return userDetails != null &&
userDetails.size() == 4 &&
password.equals(userDetails.get(0));
}
Upvotes: 0
Reputation: 2555
You can access the list like this:
for (Map.Entry<String, List<String>> curData : loginData.entrySet()) {
String username = curData.getKey();
List<String> listLoginData = curData.getValue();
String password = listLoginData.get(0);
String first_name = listLoginData.get(1);
....
}
My advice for you is using a list of object, and create a class with this parameters, like this
public class LoginData {
private String password;
private String firstName;
public LoginData(String password, String firstName){
this.password = password;
this.first_name = firstName;
}
public String getPassword(){
return password;
}
public String getFirstName(){
return firstName;
}
}
And then, use something like this:
for (Map.Entry<String, List<LoginData>> curData : loginData.entrySet()) {
String username = curData.getKey();
LoginData loginDataObject = curData.getValue();
String password = loginDataObject.getPassword();
String first_name = loginDataObject.getFirstName();
....
}
Upvotes: 1
Reputation: 28516
You can obtain values connected with specific key in HashMap
with
List<String> values = loginData.get(loginname);
Upvotes: 0