1

I'm trying to populate LinkedHashMap with for loop in order to use it in my jsf page but "put" method of hashmap overwrites the values that is held in hashmap when the new "put" method is fired.

The method is like that;

public static List<String> valuesOfEnum() throws JsonProcessingException {
        Map<String, Object> newArray = new LinkedHashMap<String, Object>();
        List<String> jsonObj = new ArrayList<String>();
        String json = null;
        for(LimanTipi limanTipi : values()){
            newArray.put("id", limanTipi.getId());
            newArray.put("value", limanTipi.getValue());
            json = new ObjectMapper().writeValueAsString(newArray);
            jsonObj.add(json);
        }
        return jsonObj;
    }

Here's the jsf code;

<f:selectItems value="#{denizlimaniViewController.limanTipleri}" var="limanTipleri" itemValue="#{limanTipleri.id}" itemLabel="#{limanTipleri.value}"/>

With this method, I convert the hashmap into list as I couldn't populate the hashmap properly but this is not what I want because I can't use this list in <f:selectItems>.

I need to use itemValue and itemLabel representing "id" and "value" properties in hashmap.

Is there a way to handle this?

Thanks

4
  • Each key must be different, use newArray.put(limanTipi.getId(), limanTipi.getValue()); Commented Jul 19, 2018 at 8:48
  • 1
    So put method doesn't overwrite if each key is different, right? @MartinSpamer Commented Jul 19, 2018 at 9:09
  • what is this limanTipleri? is this the map newArray? Commented Jul 19, 2018 at 9:14
  • 1
    @N. Doye The main problem is every time id, value keys are overwrite(that are given hard coded and after for loop execution only two key are there in map id & value and the map contain the last overwrited value). You have to create distinct key for very value(limanTipi.getValue()) value. You have to create your map something like that Map<limanTipi.getId()(datatype return by method),limanTipi.getValue()datatype return by method)> map Commented Jul 19, 2018 at 9:21

1 Answer 1

1

Key get's overwritten because you always have keys as id and value. Modify your code like below:

for(LimanTipi limanTipi : values()){
            newArray.put(limanTipi.getId(), limanTipi.getValue());
            json = new ObjectMapper().writeValueAsString(newArray);
            jsonObj.add(json);
        }

EDIT:

Hope limanTipleri is the map newArray itself. Then you need to modify your code like below:

<f:selectItems value="#{denizlimaniViewController.limanTipleri.entrySet()}" 
               var="limanTipleri" 
               itemValue="#{limanTipleri.key}" itemLabel="#{limanTipleri.value}" />
Sign up to request clarification or add additional context in comments.

1 Comment

Caused by: javax.el.PropertyNotFoundException: Property [value] not found on type [java.lang.String] This exception is thrown. May be it would be better if I add jsf codes to my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.