使用LinkedHashMap进行迭代和分配 [英] Iterating and asigning with LinkedHashMap

查看:141
本文介绍了使用LinkedHashMap进行迭代和分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有LinkedHashMap<String,List<SelectItem>> results =从数据库获得了结果,请参见

I have LinkedHashMap<String,List<SelectItem>> results = got the results from DB see here

我需要使用for循环将上述结果分配给UI中可用的列表.

I need to assign the above results to the lists available in UI using for loop.

for (Map.Entry<String, List<SelectItem>> entry : results.entrySet()) {
        String key = entry.getKey();
        List<SelectItem> values = entry.getValue();
        System.out.println("Key = " + key);
        System.out.println("Values = " + values + "n");
}

分配零件示例:

if(key.equalsIgnoreCase("getProjectManager")) {
        tempselectedProjMgrList = entry.getValue();                             
}

根据键,我将值添加到差异列表中,就像我在上面给定链接中所说的那样.

Based on the key I am adding the values to a diff list like the one i said in the given link above.

上面的sys out不打印列表内的实际值,而是像下面给出的那样打印..

The above sys out does not print the acutal values inside the list instead it prints like the one given below ..

Key = getProjectManager
Values = [javax.faces.model.SelectItem@fadb0a,javax.faces.model.SelectItem@1245c45]n
Key = getResourceOwnerSE
Values = [javax.faces.model.SelectItem@25f52c, javax.faces.model.SelectItem@323fc] <br/>

如何从上面的列表中获取实际值.

How to get the actual values from the above list.

推荐答案

SelectItem并没有覆盖从Object类继承的toString()方法:

SelectItem did'nt override the toString() method herited from the Object class which is :

getClass().getName() + '@' + Integer.toHexString(hashCode())

这就是为什么您会得到这样的输出.

That's why you get such an output.

因此,您必须遍历所有值并调用

So you'll have to loop through all the values and call getValue(). That will call the toString() method on the value object hold by the SelectItem.

System.out.println("Key = " + key);
System.out.println("Values = "); 
for(SelectItem st : values){
    System.out.print(st.getValue()+" ");
}
System.out.println();

如果您想直接获取具有关联密钥的适当列表,只需执行

If you want directly to get the appropriated list with the key associated, just do

tempselectedResOwnSeList = results.get("getProjectManager");

这篇关于使用LinkedHashMap进行迭代和分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆