Collect lists to joint list using streams

Askar Ibragimov :

I am trying to write an expression that takes data from caches (they return a List) and collect everything together.

I came up with:

List<myDto> myJointSnapshot = cachesMap.values().stream()
  .flatMap(c->c.getSnapshot()).collect(Collectors.toList());

where getSnapshot returns also List< myDto>.

This gives me an error: no instance(s) of type variable(s) R exist so that List< myDto> conforms to Stream< ? extends R>

How I can correct the expression to be valid?

Eugene :
flatMap(c->c.getSnapshot().stream())...

flatMap is expecting a Stream as a result, in your case it is a List.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113623&siteId=1