다음은 몇 가지 옵션입니다.
public static void main(String[] args) {
Map<X, List<Y>> results = new HashMap<>();
List<X> allX = getAllX();
//simple way to just replace old for loop with forEach
allX.stream().forEach(x -> {
List<Y> matchingY = getMatchingY(x.id);
sortListY(matchingY);
results.put(x, matchingY);
});
//a little bit fancier, assumes sortListY return List<Y>
allX.stream()
.map((X x) -> new AbstractMap.SimpleEntry<>(x, sortListY(getMatchingY(x.id))))
.forEach(e -> results.put(e.getKey(), e.getValue()));
//more fancy, assumes sortListY return List<Y>
Map<X, List<Y>> results2 = allX.stream()
.map((X x) -> new AbstractMap.SimpleEntry<>(x, sortListY(getMatchingY(x.id))))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
//most fancy, assumes sortListY return List<Y>
Map<X, List<Y>> results3 = allX.stream()
.collect(Collectors.toMap(Function.identity(), x -> sortListY(getMatchingY(x.id))));
//most fancy part 2, assumes sortListY return List<Y>
Map<X, List<Y>> results4 = allX.stream()
.collect(Collectors.toMap(x -> x, x -> sortListY(getMatchingY(x.id))));
}
어떤 부분을 고민하고 계십니까? –
지도의 키와 값은 무엇입니까? –
지금까지 해보신 것은 무엇입니까? – Flown