2

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){
1
  • 4
    Instead of a list it might make it a little cleaner to use an Object such as UserData which could hold password, firstname, lastname etc. Commented Jan 8, 2015 at 20:16

3 Answers 3

1

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();
    ....
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can you change the post, and put this with "correct answer",please?
0

You can obtain values connected with specific key in HashMap with

List<String> values = loginData.get(loginname);

Comments

0
    boolean validateLogin(String username, String password) {
      List<String> userDetails = loginData.get(username);
      return userDetails != null &&                
           userDetails.size() == 4 &&
           password.equals(userDetails.get(0));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.