Pergunta

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

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

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 ..

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.

Foi útil?

Solução

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();

EDIT:

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

tempselectedResOwnSeList = results.get("getProjectManager");

Outras dicas

You can do the below:

First create a toString method for your SelectItem class with all the info you want to be printed . 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");
                        }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top