By Roman Cherepanov


2018-03-13 15:56:01 8 Comments

Is it possible to map one collection to another with Java Streams, but second collection must to have different element count than first?

I have map

"1" -> 10, "2-3" -> 20

and want convert it to

List<Node> = Node(1,10), Node(2,20), Node(3,20)

How can I do this using streams?

My code is:

import com.google.common.collect.ImmutableMap;

import java.util.*;

import static java.util.stream.Collectors.toList;

public class MapOneToManyQuestion {

    public static void main(String[] args) {
        new MapOneToManyQuestion().run();
    }

    void run() {
        final Map<String, Integer> map = ImmutableMap.of("1", 10, "2-3", 20);

        List<Node> nodes = map.entrySet().stream().map(entry -> {
            if (Objects.equals(entry.getKey(), "1")) {
                return new Node(1, entry.getValue());
            } else {
                //return new Node(2, entry.getValue());
                //return new Node(3, entry.getValue());
            }
        }).collect(toList());
    }

    class Node {
        private Integer key;
        private Integer value;

        public Node(Integer key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public Integer key() {
            return this.key;
        }

        public Integer value() {
            return this.value;
        }
    }
}

1 comments

@user7 2018-03-13 16:09:04

You can use a flatMap for this

List<Node> nodes = map.entrySet()
        .stream()
        .flatMap(entry -> {
            String key = entry.getKey();
            Integer value = entry.getValue();
            return Arrays.stream(key.split("-"))
                    .map(splitKey -> new Node(Integer.valueOf(splitKey), value));
        })
        .collect(Collectors.toList());

It streams through each map element and it splits the key by - and creates one Node object for each key part resulting from the split and the map value for that entry and finally it gets collected to a list.

Related Questions

Sponsored Content

41 Answered Questions

[SOLVED] How can I initialise a static Map?

38 Answered Questions

[SOLVED] How to efficiently iterate over each entry in a Java Map?

49 Answered Questions

[SOLVED] Sort a Map<Key, Value> by values

9 Answered Questions

[SOLVED] How to Convert a Java 8 Stream to an Array?

33 Answered Questions

17 Answered Questions

[SOLVED] Java 8 List<V> into Map<K, V>

25 Answered Questions

[SOLVED] How to determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1649765 View
  • 1976 Score
  • 25 Answer
  • Tags:   java arrays

5 Answered Questions

[SOLVED] How to deserialize a list using GSON or another JSON library in Java?

  • 2010-11-30 20:49:09
  • Valter Silva
  • 83824 View
  • 97 Score
  • 5 Answer
  • Tags:   java json gson

2 Answered Questions

[SOLVED] constructor have include a void return type?

  • 2015-03-22 03:12:52
  • Naresh Kumar
  • 247 View
  • -4 Score
  • 2 Answer
  • Tags:   java

1 Answered Questions

[SOLVED] Can I have someone verify my collections for the SCJP Exam

Sponsored Content