这个问题与我在不久前发布的another有关。
我找到了一种按照我想要的方式对数据进行分组的方法。但是由于我需要对对象进行分组/映射,所以当我需要应用一些过滤时,我已经在收集过程中关闭了流。我有以下代码:
final Map<TeamDetails, List<Player>> teamDetailsPlayerListMap =
dbRows.stream().map(row -> mapDbRowToTeamPlayerPair(row))
.collect(Collectors.groupingBy(TeamPlayerPair::getKey,
Collectors.mapping(TeamPlayerPair::getValue, Collectors.toList())));
final List<Team> teams = teamDetailsPlayerListMap.entrySet().stream()
.map(teamPlayerList -> mapTeamPlayerListToTeam(teamPlayerList))
.collect(Collectors.toList());
我遇到的问题是某些dbRows对String
有空或playerName
。我不想在收集之前过滤,好像Team
没有玩家(例如只有1分贝的行,空字符串作为玩家名称),我仍然希望在我的列表中有这个最后Team
个。它只会有一个空的玩家列表。
有没有办法在收集过程中应用过滤器,以便空列或空字符串不会被添加到列表中?
我已经能够使用自定义收集器实现它,如下所示,但我只是想知道是否有一种方法可以在没有自定义收集器的情况下完成它???
Function<Player, Boolean> emptyPlayerNameFilter = new Function<Player, Boolean>() {
@Override
public Boolean apply(Player player) {
return player != null && player.getName() != null && !"".equals(player.getName());
}
};
final Map<TeamDetails, List<Player>> teamDetailsPlayerListMap =
dbRows.stream().map(row -> mapDbRowToTeamPlayerPair(row))
.collect(Collectors.groupingBy(TeamPlayerPair::getKey,
Collectors.mapping(TeamPlayerPair::getValue,
MyCollectors.toFilteredLinkedList(emptyPlayerNameFilter))));
final List<Team> finalTeams = teamDetailsPlayerListMap.entrySet().stream()
.map(teamPlayerList -> mapTeamPlayerListToTeam(teamPlayerList))
.collect(Collectors.toList());
MyCollectors.toFilteredLinkedList()
的位置:
public class MyCollectors {
public static <T, A extends Collection<T>> Collector<T, ?, A> toFilteredCollection(Supplier<A> collectionFactory, Function<T, Boolean> filter) {
return Collector.of(
collectionFactory,
(acc, entry) -> {
if (filter.apply(entry)) {
acc.add(entry);
}
},
(left, right) -> { left.addAll(right); return left; }
);
}
public static <T> Collector<T, ?, List<T>> toFilteredLinkedList(Function<T, Boolean> filter) {
return toFilteredCollection(LinkedList<T>::new, filter);
}
}
答案 0 :(得分:4)
似乎您需要一个filtering
收集器,它类似于mapping
,但执行过滤。它在Java-9中已经是implemented,但在Java-8中却没有。您可以将它放入项目中的某个实用程序类中:
public static <T, A, R> Collector<T, A, R> filtering(
Predicate<? super T> filter, Collector<T, A, R> downstream) {
BiConsumer<A, T> accumulator = downstream.accumulator();
Set<Characteristics> characteristics = downstream.characteristics();
return Collector.of(downstream.supplier(), (acc, t) -> {
if(filter.test(t)) accumulator.accept(acc, t);
}, downstream.combiner(), downstream.finisher(),
characteristics.toArray(new Collector.Characteristics[0]));
}
并像这样使用:
final Map<TeamDetails, List<Player>> teamDetailsPlayerListMap =
dbRows.stream()
.map(row -> mapDbRowToTeamPlayerPair(row))
.collect(Collectors.groupingBy(TeamPlayerPair::getKey,
Collectors.mapping(TeamPlayerPair::getValue,
filtering(player -> player != null && player.getName() != null
&& !player.getName().isEmpty(), Collectors.toList()))));
如果您不想手动将filtering
添加到项目中,可以使用一些提供此类收集器的第三方库,例如MoreCollectors.filtering()
,这可以在{{3}中找到库。