如何迭代地减去和比较java中的值

时间:2015-02-18 10:23:01

标签: java

有n个指标值,如下所示

metric of feature clear 0.4054651081081644
metric of board various 0.6931471805599453
metric of design few 0.025975486403260736
metric of call end 0.13353139262452257
metric of bag other 0.1823215567939546

现在,必须采取最高价值(各种各样)。从最高值开始迭代地逐个减去所有其他值并检索高于0.02的值。

2 个答案:

答案 0 :(得分:1)

List<Double> vals = <list of your values>

double max = Collections.max(vals);

vals.remove(max);
double result = vals.stream()
                    .filter(v -> v > .02)
                    .reduce(max, (v1, v2) -> v1 - v2);

答案 1 :(得分:0)

伪代码(实际上是Python):

metrics = [0.4054651081081644, 0.6931471805599453, ... 0.1823215567939546]
mx = max(metrics)
for m in metrics:
    if m != mx and m > 0.02:
        mx = mx - m  

将Java翻译留给读者。

警告:比较浮点数可能会很棘手。