使用Java 8 Streams,如何在HashMap中查找给定元素的最大值
|
我有一个产品的HashMap.每个产品都有价格.我知道如何找到具有最高价格的产品.但是使用 Java 8 Streams真的让我很困惑.我试过这个但没有运气: public Product getMostExpensiveProduct(HashMap<Integer,Product> items) {
Product maxPriceProduct = items.entrySet()
.stream()
.reduce((Product a,Product b) ->
a.getPrice() < b.getPrice() ? b : a);
return maxPriceProduct;
}
解决方法第一个问题是,由于您希望按价格找到最大产品,因此最好使用items.values()作为流的来源,然后您将拥有Stream< Product>而不是Stream< Map.Entry< Integer,Product>>.其次,reduce操作没有正确的类型.因此,要使当前代码正常工作,您需要执行以下操作: Optional<Map.Entry<Integer,Product>> result =
items.entrySet()
.stream()
.reduce((Map.Entry<Integer,Product> a,Map.Entry<Integer,Product> b) ->
a.getValue().getPrice() < b.getValue().getPrice() ? b : a);
return result.isPresent() ? result.get().getValue() : null;
第三,reduce操作的这种重载返回Optional< T>.所以结果集的接收者类型必须是Optional< T>如上所示. 在上面,我们在Optional中没有值的情况下返回null. 一个更好的解决方案是使方法返回一个类型Optional< Product>.这将向您或您的同事以及您的方法的所有未来用户提供文档,其中可能会给出结果为空的值. 这是一个更好的替代方法,可以在文档中返回null,并确保此方法的用户安全地解包返回值. 无效在某些时候可能是危险的,并且在适当的情况下利用Optional可以带您走很长的路. 考虑到所有这些,您的代码将成为: // example without returning an `Optional<T>`
public Product getMostExpensiveProduct(HashMap<Integer,Product> items) {
Optional<Product> maxPriceProduct =
items.values()
.stream()
.reduce((Product a,Product b) ->
a.getPrice() < b.getPrice() ? b : a);
return maxPriceProduct.orElse(null);
}
//示例返回一个Optional< T> public Optional<Product> getMostExpensiveProduct(HashMap<Integer,Product> items) {
Optional<Product> maxPriceProduct =
items.values()
.stream()
.reduce((Product a,Product b) ->
a.getPrice() < b.getPrice() ? b : a);
return maxPriceProduct;
}
无论如何,max方法更适合于此任务而不是reduce,因此可以将其全部改进为: Optional<Product> maxPriceProduct =
items.values()
.stream()
.max(Comparator.comparingInt(Product::getPrice)); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
