简体   繁体   English

如何从Java中的哈希列表中检索值

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

hi i am very new to java. 嗨,我对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":" * "}]** [{“电子邮件”:“ xxx”,“密码”:“ * ”}] **

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") . hashes.get(0).get("password")

can we do this in some better way instead of hardcoding with index 0 here. 我们能否以更好的方式做到这一点,而不是在这里用索引0进行硬编码。 Could any one please suggest me with the code. 任何人都可以用代码建议我。

Use POJO instead of using HashMap<String, String> 使用POJO而不是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM