This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, Map<Thing, Boolean>> consumerWithEachDeviceStatus = consumerList | |
.stream() | |
.flatMap(consumer -> consumer.getThings() | |
.stream() | |
.map(thing -> new AbstractMap.SimpleImmutableEntry<>(consumer.getName(), thing)) | |
) | |
.collect(Collectors.groupingBy(entry -> entry.getKey(), | |
Collectors.toMap(AbstractMap.SimpleImmutableEntry::getValue, entry -> entry.getValue().isRunning()))); | |
System.out.println(consumerWithEachDeviceStatus); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean bobAllDevicesRunning = consumerList | |
.stream() | |
.filter(consumer -> "Bob".equals(consumer.getName())) | |
.flatMap(consumer -> consumer.getThings().stream()) | |
.allMatch(Thing::isRunning); | |
System.out.println(bobAllDevicesRunning); | |
// Output : true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean isAlDevicesRunnig = consumerList | |
.stream() | |
.flatMap(consumer -> consumer.getThings().stream()) | |
// 'allMatch' is a terminal operation | |
.allMatch(Thing::isRunning); | |
System.out.println(isAlDevicesRunnig); | |
// Output : true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, Integer> consumerWithTotalAmount = consumerList | |
.stream() | |
.flatMap(consumer -> consumer.getThings() | |
.stream() | |
.map(thing -> new AbstractMap.SimpleImmutableEntry<>(consumer.getName(), thing.getCost())) | |
).collect(Collectors.groupingBy(AbstractMap.SimpleImmutableEntry::getKey, | |
Collectors.mapping(entry -> entry.getValue(), Collectors.summingInt(value -> value)))); | |
System.out.println(consumerWithTotalAmount); | |
// Output: {Bob=680, Satti=360, Sri=320, Malli=180} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<Type, Set<String>> allUsersByDeviceType = consumerList | |
.stream() | |
.flatMap(consumer -> consumer.getThings() | |
.stream() | |
.map(thing -> new AbstractMap.SimpleImmutableEntry<>(thing.getType(), consumer.getName())) | |
).collect(Collectors.groupingBy(AbstractMap.SimpleImmutableEntry::getKey, | |
Collectors.mapping(entry -> entry.getValue(), Collectors.toSet())) | |
); | |
System.out.println(allUsersByDeviceType); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to loop over nested collections referring to parent elements? | |
List<Consumer> allConsumersWhoHasHealthDevice = consumerList | |
.stream() | |
.flatMap(consumer -> consumer.getThings().stream() | |
.filter(thing -> thing.isRunning() && Type.HEALTH == thing.getType()) | |
// This is cool!!! | |
.map(thing -> new AbstractMap.SimpleImmutableEntry<>(consumer, thing)) | |
) | |
.map(entry -> entry.getKey()) | |
.collect(Collectors.toList()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
consumerList | |
.stream() | |
// map intermediate operation | |
// 'map' is used to transform one element type into other, here we transformed Consumer into String | |
.map(Consumer::getName) | |
// 'distinct' intermediate operation | |
.distinct() | |
// 'sorted' intermediate operation | |
.sorted() | |
// 'reduce' terminal operation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean isAnyConsumerInBerlin = consumerList | |
.stream() | |
// anyMatch terminal operation | |
.anyMatch(consumer -> "Berlin".equals(consumer.getCity())); | |
System.out.println(String.format("Are there any user based in Berlin: %b", isAnyConsumerInBerlin)); | |
// Output: Are there any user based in Berlin:true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> uniqueCities = consumerList | |
.stream() | |
.map(Consumer::getCity) | |
.distinct() | |
.collect(Collectors.toList()); | |
System.out.println(uniqueCities); | |
// Output: [Hyd, Berlin] |