0

I have List and I want to convert to Map<Integer,String> using streams in java8.

Say for example : 
List<Integer> li = Arrays.asList(1,2,3);

Then want to convert to Map<Integer,String> like 
Map({1,"1"},{2,"2"},{3,"3"})

2 Answers 2

1

You can try below stuff and should work fine(Tested).

    List<Integer> li = Arrays.asList(1,2,3);
    Map<Integer, String> result =      
    li.stream().collect(Collectors.toMap(i -> i, i -> i.toString()));
Sign up to request clarification or add additional context in comments.

Comments

0

If you want map, there should be Key=Value pair, I assume you [{1,"1"}, {2,"2"}, {3,"3"}] want like this,

List<String> collect = li.stream()
                          .map(a -> "{"+a + ",\"" + a +"\"}")
                          .collect(Collectors.toList());

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.