0

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.

Community
  • 1
  • 1
Karthik
  • 371
  • 3
  • 7
  • 30
  • Answered in: [Printing Java collections nicely (toString doesn't return pretty output)](http://stackoverflow.com/questions/395401/printing-java-collections-nicely-tostring-doesnt-return-pretty-output) – Oleg Sklyar Jan 07 '14 at 11:51
  • I tried that too but its not working for me.Is there any other option to iterate and get the list ? – Karthik Jan 07 '14 at 11:59

2 Answers2

1

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

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 1
    Thanks its printing correctly!!.But based on the keys I have to assign the values to different list ex: xxlist,yylist,zzlist.But from the DB I have already retrieved the list and put them into the hashmap and brought it into UI.But why do I have to again iterate and get the values don't I have any other option to get the list directly? Instead iterating ?Please advise – Karthik Jan 07 '14 at 12:38
  • Thanks its printing correctly!!.But based on the keys I have to assign the values to different list ex: xxlist,yylist,zzlist.But from the DB I have already retrieved the list and put them into the hashmap and brought it into UI.But why do I have to again iterate and get the values don't I have any other option to get the list directly? Instead iterating ?Please advise.Link for retreiving the list is [link](http://stackoverflow.com/questions/20970793/iterating-and-asigning-with-linkedhashmap)here – Karthik Jan 07 '14 at 12:44
  • @techy360 What do you mean ? Please edit or ask a new question with a concrete example. – Alexis C. Jan 07 '14 at 12:46
  • I already did List getProjectManager = new ArrayList();results.put("getProjectManager", getProjectManager); in the DAO and I got the results in LinkedHashMap> results. Can't I able to assign the diff lists direclty like xxlist =results.getvalue() based on key say example : if(key.equalsIgnoreCase("getResourceOwnerSE ")){tempselectedResOwnSeList = entry.getValue(); } in UI ?; instead looping ? I am trying to assign the results directly to a list.My intention is to increase the performance. – Karthik Jan 07 '14 at 12:53
  • @techy360 If I understand well you can directly get the appropriated list with the key. I.e `tempselectedResOwnSeList = results.get("getProjectManager");`. The get method will give you the value for the associated key in the map. – Alexis C. Jan 07 '14 at 12:57
  • Thats really a stupid question from me.I should have known that results.get("getProjectManager"); will obivously give the lists.But unfortunately my mind isn't working that time.Thanks a lot ZouZou it resolved my issue. – Karthik Jan 08 '14 at 06:16
  • @techy360 You're welcome! If you have a doubt, always check the doc ! http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html – Alexis C. Jan 08 '14 at 08:12
0

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");
                        }
}
Dimitrios Douras
  • 506
  • 4
  • 16
  • I tried the above one too but I am getting the following output Key = getProjectManager Values = javax.faces.model.SelectItem@94e3c1njavax.faces.model.SelectItem@f4acd1njavax.faces.model.SelectItem@17e0e74njavax.faces.model.SelectItem@e71be2njavax.faces.model.SelectItem@d79805njavax.faces.model.SelectItem@3d96a6njavax.faces.model.SelectItem@16d749enjavax.faces.model.SelectItem@1034f8dnjavax.faces.model.SelectItem@5bb9aenjavax.faces.model.SelectItem@1fb6021nKey – Karthik Jan 07 '14 at 12:16
  • Did you create the toString method in your SelectItem class? – Dimitrios Douras Jan 07 '14 at 12:16
  • SelectItem Class is in javax.jsf_1.2.0 jar and its not editable – Karthik Jan 07 '14 at 12:19
  • Then you need to follow zouzou advice and use getValue() – Dimitrios Douras Jan 07 '14 at 12:21
  • Appreciate if you could provide me some links or URL's for the solution – Karthik Jan 07 '14 at 12:23