循环列表以获取密钥&使用Java的值对

时间:2015-06-26 09:19:00

标签: java iteration

这是我收到的回复:

[{rollno=1066276530, rollvalue=1815401000238}] 

这是强制转换为List<Map<String, String>> leadIds。现在我想迭代这些leadIds以获得关键&amp;价值对。我已经尝试过这种方式,但是返回键为“1066276530”,值为“rollno”,而我期望键为“1066276530”,值为“1815401000238”。

这是我的代码

for (Map<String, String> map : leadIds) {
    for (Map.Entry<String, String> entry : map.entrySet()) {
        String rollno = String.valueOf(entry.getValue());
        String rollvalue = String.valueOf(entry.getKey());
        LOG.info("[Status Search] Roll No:" +rollno
            + " Roll value:" + rollvalue);
    }
}

3 个答案:

答案 0 :(得分:3)

查看您的日志声明。您已切换了号码和ID:

LOG.info("[Status Search] Application ID :"+applicationNumber+
    " Application Number :"+appId);

应该是:

LOG.info("[Status Search] Application ID :"+appId+
    " Application Number :"+applicationNumber);

答案 1 :(得分:0)

试试这个

 for (Map<String, String> map : leadIds) {
    for (Map.Entry<String, String> entry : map.entrySet()) {
        String applicationNumber = String.valueOf(entry.getkey());
        String appVal = String.valueOf(entry.getValue());
        LOG.info("[Status Search] Application ID :"+applicationNumber+" Application Number :"+appVal);
}

}

答案 2 :(得分:0)

Actually you don't need two loops to achieve this, your outer loop map contains the values for the keys rollno and rollvalue, so get it from there.

for (Map<String, String> map : leadIds) {
    String applicationNumber = map.get("rollno");
    String appId = map.get("rollvalue");
    LOG.info("[Status Search] Application ID:"+ applicationNumber + " Application Number:" + appId);
}