简体   繁体   English

使用LinkedHashMap进行迭代和分配

[英]Iterating and asigning with LinkedHashMap

I have LinkedHashMap<String,List<SelectItem>> results = got the results from DB see here LinkedHashMap<String,List<SelectItem>> results =得到了来自DB的结果看这里

I need to assign the above results to the lists available in UI using for loop. 我需要使用for循环将上述结果分配给UI中可用的列表。

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");
}

Assigning part example : 分配零件示例:

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. 根据密钥,我将这些值添加到差异列表中,就像我在上面给定链接中所说的那样。

The above sys out does not print the acutal values inside the list instead it prints like the one given below .. 上面的sys out不打印列表内的实际值,而是像下面给出的那样打印。

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 did'nt override the toString() method herited from the Object class which is : SelectItem不会覆盖从Object类继承的toString()方法,该方法是:

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() . 因此,您必须遍历所有值并调用getValue() That will call the toString() method on the value object hold by the SelectItem . 这将在SelectItem持有的值对象上调用toString()方法。

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

EDIT: 编辑:

If you want directly to get the appropriated list with the key associated, just do 如果您想直接获取具有关联密钥的适当列表,只需执行

tempselectedResOwnSeList = results.get("getProjectManager");

You can do the below: 您可以执行以下操作:

First create a toString method for your SelectItem class with all the info you want to be printed . 首先为SelectItem类创建一个toString方法,其中包含要打印的所有信息。 For example: 例如:

public class SelectItem {

private int a;
private String b;

@Override
public String toString() {
    return "SelectItem [a=" + a + ", b=" + b + "]";
}

} }

then do: 然后做:

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.print("Values = ");}
                        for (SelectItem selectItem : values){
                            System.out.print(selectItem.toString() +    "n");
                        }
}

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

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