流映射多种类型
Stream mapping multiple types
如何将此代码转换为使用流的代码?一行,如果可能的话:
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));
好吧,IMO“在一行中”不是使用 Stream
的好点,因为它们是有代价的,但你可以这样做:
return ImmutableList.of( houseConstructor.newInstance( 5, Houses.all() .entrySet() .stream() .map(entry -> new Key<>(entry.getKey(), HousesTypes.getFor(entry.getValue())) .collect(Collectors.toList()) ) );