Skip to content

Instantly share code, notes, and snippets.

@Jayasagar
Created December 17, 2017 15:15
Show Gist options
  • Save Jayasagar/17a73d9ec8e19c3a70dc97d45372ccad to your computer and use it in GitHub Desktop.
Save Jayasagar/17a73d9ec8e19c3a70dc97d45372ccad to your computer and use it in GitHub Desktop.
// How to loop nested collection using lambda streams?
Set<Event> allActiveDeviceOperations = consumerList
.stream()
// 'flatMap' intermediate operation, here produces the stream of user Things(Devices)
.flatMap(consumer -> consumer.getThings().stream())
.filter(thing -> thing.isRunning())
.flatMap(thing -> thing.getEvents().stream())
.collect(Collectors.toSet());
System.out.println(allActiveDeviceOperations);
//Output : [Event(name=motionCaptured), Event(name=switchedOff), Event(name=switchedOn), Event(name=virusFound), Event(name=dryField)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment