![](/img/trans.png)
[英]Fetch max value in a list of objects which are inside list of objects by month using Java Stream
[英]Java 8: How to get particular value within object inside list without converting stream back to list and fetch value at 0th location?
我需要从链接列表中的href()
返回值,其中 Rel 值为String
。
class Links{
String rel;
String href;
}
class 2{
List<Links> links
}
以下代码正在这样做,但看起来并不酷
return links.stream().filter(d -> StringUtils.equalsIgnoreCase(d.getRel(), "Self")).collect(Collectors.toList()).get(0).getHref();
有什么方法可以直接从列表中获取getHref
而不是converting it back to list
并get 0th element
。
是的,使用findFirst()
:
return links.stream()
.filter(d -> StringUtils.equalsIgnoreCase(d.getRel(), "Self"))
.findFirst() // returns an Optional<Links>
.map(Links::getHref) // returns an Optional<String>
.orElse(null); // returns String (either getHref of the found Links instance, or
// null if no instance passed the filter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.