How can I convert this code to the one that uses streams? one line, if possible:
List<Key<String, ?>> keys = Lists.newArrayList();
for (Map.Entry<String, House> entry : Houses.all().entrySet()) {
keys.add(new Key<>(entry.getKey(),
HousesTypes.getFor(entry.getValue()));
}
return ImmutableList.of(houseConstructor.newInstance(5, keys));
Advertisement
Answer
Well, IMO “in one line” is not a great point to use Stream because they came at a cost, but you can do something like this:
return ImmutableList.of(
houseConstructor.newInstance(
5,
Houses.all()
.entrySet()
.stream()
.map(entry -> new Key<>(entry.getKey(), HousesTypes.getFor(entry.getValue()))
.collect(Collectors.toList())
)
);