Reputation: 3
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));
Upvotes: 0
Views: 431
Reputation: 12929
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()) ) );
Upvotes: 2