32

I have a class like this

public class Example {
    private List<Integer> ids;

    public getIds() {
        return this.ids; 
    }
}

If I have a list of objects of this class like this

List<Example> examples;

How would I be able to map the id lists of all examples into one list? I tried like this:

List<Integer> concat = examples.stream().map(Example::getIds).collect(Collectors.toList());

but getting an error with Collectors.toList()

What would be the correct way to achive this with Java 8 stream api?

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Jakob Abfalter
  • 4,980
  • 17
  • 54
  • 94
  • 3
    Possible duplicate of [What's the difference between map and flatMap methods in Java 8?](http://stackoverflow.com/questions/26684562/whats-the-difference-between-map-and-flatmap-methods-in-java-8). And even it is not a perfect duplicate, the answers there are useful. – Robin Topper Apr 28 '17 at 14:05
  • 6
    Your method `getIds` does not have a return type. – devang Apr 28 '17 at 14:05

3 Answers3

56

Use flatMap:

List<Integer> concat = examples.stream()
    .flatMap(e -> e.getIds().stream())
    .collect(Collectors.toList());
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Can you explain the difference between `flatMap` and `map` – CraigR8806 Apr 28 '17 at 14:06
  • 6
    @CraigR8806 Extensive explanations [here](http://stackoverflow.com/questions/26684562/whats-the-difference-between-map-and-flatmap-methods-in-java-8) – Robin Topper Apr 28 '17 at 14:08
  • 1
    What would happen if `e.getIds()` returns an empty list or null? How could a test for empty list or null be included into the statement? – gabriel Aug 24 '17 at 10:08
  • 1
    @gabriel "empty list" well, there's nothing in the list, but it would work just the same. "null" a `NullPointerException` would be thrown. How would you want to handle null; and why would you want to (specially) handle an empty list? – Andy Turner Aug 24 '17 at 10:13
  • Ok, yes for an empty list you're right, it won't be a big problem. Concerning `null`: I wondered, what the lambda expression should/could return if I checked there if the list was `null` and to avoid a `NullPointerException`. – gabriel Aug 24 '17 at 10:17
  • 1
    Well you could do something like `{ List ids = e.getIds(); if (ids == null) ids = Collections.emptyList(); return ids.stream(); }`. But consider that the null might itself be a bug; so it might be better to fix it rather than work around it. – Andy Turner Aug 24 '17 at 10:22
  • Sure, you're right. Will have to look into that as well. Still, thank you for your effort! +1 – gabriel Aug 24 '17 at 10:24
  • you can use something like this .filter(Objects::nonNull) .. so you would get only notNull. – Manglesh Mar 22 '23 at 10:49
10

Another solution by using method reference expression instead of lambda expression:

List<Integer> concat = examples.stream()
                               .map(Example::getIds)
                               .flatMap(List::stream)
                               .collect(Collectors.toList());
holi-java
  • 29,655
  • 7
  • 72
  • 83
0

you can use flatMap alternative for stream.map