小编jpd*_*ond的帖子

使用Stream从HashSet中排除极值

我一直在试验Java 8流,这是删除最小和最大分数的最佳方法.

private final Set<MatchScore> scores = new HashSet<>(10);

. . .

public double OPR() {
    return scores.stream()
            .mapToDouble(MatchScore::getScore)
            .filter((num) -> { //Exclude min and max score
                return num != scores.stream()
                                    .mapToDouble(MatchScore::getScore)
                                    .max().getAsDouble() 
                        && 
                       num != scores.stream()
                                    .mapToDouble(MatchScore::getScore)
                                    .min().getAsDouble();
            })
            .average().getAsDouble();
}
Run Code Online (Sandbox Code Playgroud)

java performance java-8 java-stream

4
推荐指数
1
解决办法
249
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1

performance ×1