1

Given list of strings like this:

"Y:Yes",
"N:No",
"A:Apple"

I have something like

Map updated = values.stream().map(v -> v.split(":")).collect(Collectors.toMap(v1 -> v1[0],v1->v1.length>1?v1[1]:v1[0]));

But this gives me map as:

{
"Y":"Yes",
"N":"No",
"A":"Apple"
}

How can I get a list of maps as such:

[
{
  name:"Y",  
  display:"Yes"
},
{
  name:"N",  
  display:"No"
},
{
  name:"A",  
  display:"Apple"
}
]
5
  • Possible duplicate of Java 8 List<V> into Map<K, V> Commented Oct 31, 2017 at 4:01
  • What's wrong with what you have? Commented Oct 31, 2017 at 4:02
  • @erickson Hi I updated my question to be more descriptive. Commented Oct 31, 2017 at 4:17
  • @nullpointer hi, I looked at the other question, doesn't help my problem. I'd like to have a list of maps with custom keys. Commented Oct 31, 2017 at 4:18
  • @user1491987 Whats your current DS to represent the list? Please update the question with the current List<> and output List<> types and what's the error with your piece of code as well. Commented Oct 31, 2017 at 4:20

3 Answers 3

2

If you are using Java 9, you can use the new immutable map static factory methods, as follows:

List<Map<String, String>> updated = values.stream()
    .map(v -> v.split(":"))
    .map(a -> Map.of("name", a[0], "display", a[1]))
    .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

1

As you want to get a List, not a map, your last function call cannot be Collectors.toMap, needs to be Collectors.toList. Now, each invocation to the map method should generate a new Map, so something like this would do:

List updated = values.stream()
    .map(v -> {
        String[] parts = v.split(":");
        Map<String, String> map = new HashMap<>();
        map.put("name", parts[0]);
        map.put("display", parts[1]);
        return map;
    )
    .collect(Collectors.toList());

Some people would prefer:

List updated = values.stream()
    .map(v -> {
        String[] parts = v.split(":");
        return new HashMap<>() {{
            put("name", parts[0]);
            put("display", parts[1]);
        }};
    )
    .collect(Collectors.toList());

which creates an extra helper class. Or if you can use Guava:

List updated = values.stream()
    .map(v -> {
        String[] parts = v.split(":");
        return ImmutableMap.of("name", parts[0], "display", parts[1]);
    )
    .collect(Collectors.toList());

BTW: In the examples I used Listbut the complete type of what you describe would be List<Map<String, String>>.

2 Comments

I am not sure, how you collect toList and end up with a Map
you meant the type of the variable! fixed, needs to be a List
1

You can use following if you're still using Java8, if you happen to use Java9 then have a look at Federicos answer:

final List<Map<String,String>> updated = values.stream()
    .map(v -> v.split(":"))
    .map(arr -> {
        Map<String, String> map = new HashMap<>();
        map.put("name", arr[0]);
        map.put("display", arr[1]);
        return map;
    })
    .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.