1

I have method that should return Map<Strings, List<String>> but in the mean time my method gives me a Map<Strings, Object>, I want to transfer the values of object into a List of Strings.

Here is the current code:

    @SuppressWarnings("unchecked")
    static Map<String, List<String>> getQueryParameters(JsonObject inputJsonObject) {

        JsonArray parameters = inputJsonObject.getJsonArray("parameters");

        Optional<JsonObject> queryParameters = parameters.stream().
            filter(JsonObject.class::isInstance).
            map(JsonObject.class::cast).
            filter(jsonObject -> jsonObject.getJsonObject("queryParameters") != null).
            map(item -> item.getJsonObject("queryParameters")).findFirst();

        Map<String, Object> paramMap = queryParameters.get().getMap();

paramMap contains key and value , values could be an arrays of integers so I want to put them into the map below:

 Map<String, List<String>> mystore = new LinkedHashMap<String, List<String>>();

My solution is this which did not work correctly

 Map<String, List<String>> mystore = new LinkedHashMap<String, List<String>>();

        Map<String, Object> paramMap = queryParameters.get().getMap();

        Iterator it = paramMap.entrySet().iterator();

          while (it.hasNext()) {
            String key = it.next().toString();
            if (!mystore.containsKey(key)) ;

            mystore.put(key, new LinkedList<String>());

            mystore.get(key).add(it.next().toString());
        }

I was a key holding another key as value and is just a mix up , any suggestions? After debuging what happens i see that mystore holds both "key and value" together as a key and value it hold the next "key and value as value

2 Answers 2

2

Should be something like this:

while (it.hasNext()) {
        Map.Entry<String, Object> next = iterator.next();
        String key = next.getKey();
        Object value = next.getValue();
        if (!mystore.containsKey(key)) mystore.put(key, new LinkedList<String>());
        mystore.get(key).add(value.toString());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

iterator.nexr() need to be casted like this (Map.Entry<String, Object>) and also i think you mean it.next as was written in while statement
it.next returns the the next key-value entry of the map. No cast should be needed.
1

I'm not writing a program for you, but instead help you in finding a problem. You are confused with Entry. If you are using IDE, you should solve it easier. Look for this line :

String key = it.next().toString();

Entry has a K,V pair. The iterator returns an EntrySet and thus usage to get key is it.next().getKey() and it.next().getValue()

Now that you have a correct key, please go on debugging. Instead of putting and getting and manipulating in below lines of your code. Put with correct value instead?

Yours:

mystore.put(key, new LinkedList<String>());
mystore.get(key).add(it.next().toString());

What about?:

Entry entry = it.next();
//Get key and value here. DO coding using Entry's methods
List<String> ll = new LinkedList<String>();
ll.add(value)
mystore.put(key, ll);

Tip: Always have the Javadoc or reference documentation handy for knowing more. That's how you learn the language. Refer:https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html

Comments

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.