Skip to content

Instantly share code, notes, and snippets.

@Jayasagar
Created December 17, 2017 15:11
Show Gist options
  • Save Jayasagar/e89406b7a586e3226501f465924edb5e to your computer and use it in GitHub Desktop.
Save Jayasagar/e89406b7a586e3226501f465924edb5e to your computer and use it in GitHub Desktop.
Map<String, List<Thing>> consumerDevicesByAge =
consumerList
.stream()
// filter is an intermediate operation
.filter(consumer -> consumer.getAge() > 30)
// collect is a terminal operation
.collect(Collectors.toMap(Consumer::getName, Consumer::getThings));
// In the above code, used 'Method reference(shorthand form of lamda)' Consumer::getName which is equalent to consumer -> consumer.getName()
System.out.println(consumerDevicesByAge);
// Output: {Bob=[Thing(name=Bulb), Thing(name=MotionDetector), Thing(name=AgriFieldSensor), Thing(name=EyeVirusDetector)], Sri=[Thing(name=Bulb), Thing(name=EyeVirusDetector)]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment