Skip to content

Instantly share code, notes, and snippets.

View Jayasagar's full-sized avatar

Jayasagar Jagirapu Jayasagar

View GitHub Profile
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);
boolean bobAllDevicesRunning = consumerList
.stream()
.filter(consumer -> "Bob".equals(consumer.getName()))
.flatMap(consumer -> consumer.getThings().stream())
.allMatch(Thing::isRunning);
System.out.println(bobAllDevicesRunning);
// Output : true
boolean isAlDevicesRunnig = consumerList
.stream()
.flatMap(consumer -> consumer.getThings().stream())
// 'allMatch' is a terminal operation
.allMatch(Thing::isRunning);
System.out.println(isAlDevicesRunnig);
// Output : true
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}
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);
// 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());
// 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)]
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
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
List<String> uniqueCities = consumerList
.stream()
.map(Consumer::getCity)
.distinct()
.collect(Collectors.toList());
System.out.println(uniqueCities);
// Output: [Hyd, Berlin]