guava - Concatenate Collections with Java 8 -


i want iterate on collection of collections. guava this:

import static com.google.collections.iterables.*;  class group {     private collection<person> persons;     public collection<person> getpersons(); }  class person {     private string name;     public string getname(); }  collection<group> groups = ...; iterable<person> persons = concat(transform(groups, group::getpersons())); iterable<string> names = transform(persons, person::getname); 

but how can same thing java 8 streams?

groups.stream().map(group::getpersons())...? 

you can achieve flat mapping elements of stream stream.

let me explain code:

groups.stream()         .flatmap(group -> group.getpersons().stream()); 

what here, is:

  1. obtain stream<collection<group>>.
  2. then flat map every obtained stream<person>, group, original stream, of type stream<person>.

now after flatmap(), can whatever want obtained stream<person>.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -