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"
}
]
shmosel
  • 49,289
  • 6
  • 73
  • 138
user1491987
  • 751
  • 1
  • 14
  • 34
  • Possible duplicate of [Java 8 List into Map](https://stackoverflow.com/questions/20363719/java-8-listv-into-mapk-v) – Naman Oct 31 '17 at 04:01
  • What's wrong with what you have? – erickson Oct 31 '17 at 04:02
  • @erickson Hi I updated my question to be more descriptive. – user1491987 Oct 31 '17 at 04: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. – user1491987 Oct 31 '17 at 04: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. – Naman Oct 31 '17 at 04:20

3 Answers3

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());
fps
  • 33,623
  • 8
  • 55
  • 110
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>>.

Daniel
  • 21,933
  • 14
  • 72
  • 101
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());
Lino
  • 19,604
  • 6
  • 47
  • 65