user2458408
user2458408

Reputation: 79

how to retrieve the values from a list of hashes in java

hi i am very new to java. In my code one of my method does this

List<HashMap<String, String>> hashes = db.getValue("LoginUser");

which returns a list of hashes

[{"email":"xxx","password":"*"}]**

the main thing i want to know is how to use the key value from this hash.

the way i followed to get the value of the key email is as such:

hashes.get(0).get("email")

and to get the value of key password is as such:

hashes.get(0).get("password").

can we do this in some better way instead of hardcoding with index 0 here. Could any one please suggest me with the code.

Upvotes: 0

Views: 683

Answers (1)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26084

Use POJO instead of using HashMap<String, String>

class UserDetails{
     String email;
     String password;
     //Setters and Getters
}

List<UserDetails> hashes = db.getValue("LoginUser");

UserDetails userDetails = hashes.get(0);

userDetails.getEmail();
userDetails.getPassword();

Upvotes: 1

Related Questions