我有一张 map Map<String, List<Double>
我想在所有列表中找到最大(或最小)值。
该函数应返回最大(或最小)值以及属于该值的键。
签名可能是
public static Pair<String,Double> getKeyValue(Map<String, List<Double>> map, BinaryOperator<Double> function)
获取 map 和函数Double::max
或 Double::min
我如何使用 java 8 stream api 高效(并且漂亮地)实现它?
最佳答案
A BinaryOperator
不是该任务的良好规范,可以直接用于减少以产生适当的值,例如最小值或最大值,但它不适合返回关联值,如 Map
的关键值(value)。以这种方式使用它意味着实现必须执行额外的操作来找出 BinaryOperator
是什么实际上是为了在缩减过程中选择正确的键值。更糟糕的是,它不能保证 BinaryOperator
做一些允许执行这种减少的事情,例如运算符可能会返回一个既不是其参数也不是其参数的值。
对于这样的任务,Comparator
是更好的选择,因为它旨在指定排序并执行相关操作,如查找最大值和最小值。实现可能如下所示:
public static Pair<String,Double> getMinimumKeyValue(
Map<String, List<Double>> map, Comparator<Double> function) {
return map.entrySet().stream()
.map(e->new Pair<>(e.getKey(), e.getValue().stream().min(function).get()))
.min(Comparator.comparing(Pair::getRight, function)).get();
}
它被命名为getMinimumKeyValue
因为当您传入 Comparator.naturalOrder()
时它将返回最小键/值对.
但是您也可以通过传递 Comparator.reverseOrder()
来获得最大值.
而且很容易修改以支持更广泛的用例:
public static <K,V> Pair<K,V> getMinKeyValue(
Map<K, ? extends Collection<V>> map, Comparator<? super V> function) {
return map.entrySet().stream()
.map(e->new Pair<>(e.getKey(), e.getValue().stream().min(function).get()))
.min(Comparator.comparing(Pair::getRight, function)).get();
}
这仍然适用于获得 Pair<String,Double>
从一个Map<String, List<Double>>
但可以做更多……
关于Java 8 HashMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26327239/