解析流函数中的对象
Parsing an object in stream function
如何在流函数中将 String 解析为 BigDecimal,我想将 属性 设置为 String,然后将其解析为 BigDecimal,最后通过计算从每个函数中获取的总和来获得总和目的。 (目前在BigDecimal::add上有一条消息无法解析方法添加)谢谢。
public Optional<BigDecimal> generalTotalForSingleInvoice(PDDocument pdDocument) {
return Optional.of(new BigDecimal(findFirst(extractText(pdDocument), INVOICE_GENERAL_TOTAL)
.replaceAll("\.", "")
.replaceAll(",", ".")).stripTrailingZeros());
}
public Optional<BigDecimal> sumOfGeneralTotal(List<PDDocument> document) {
return document.stream().map(this::generalTotalForSingleInvoice).reduce(BigDecimal.ZERO, BigDecimal::add);
}
排除基于generalTotalForSingleInvoice
实现的return类型,你仍然可以使用filter
和map
来确保只有PDDocument
哪个存在有助于在你的减少中积累:
return document.stream()
.map(this::generalTotalForSingleInvoice)
.filter(Optional::isPresent)
.map(Optional::get)
.reduce(BigDecimal::add);
您正在创建一个 Stream<Optional<BigDecimal>>
,并且您不能用 BinaryOperator<BigDecimal>
减少 Stream
,所以 BigDecimal::add
不适合。
因为你的 generalTotalForSingleInvoice
永远不能 return 一个空的 Optional
,它没有理由 return 一个 Optional
。您可以将其更改为:
public BigDecimal generalTotalForSingleInvoice(PDDocument pdDocument) {
return new BigDecimal(findFirst(extractText(pdDocument), INVOICE_GENERAL_TOTAL)
.replaceAll("\.", "")
.replaceAll(",", ".")).stripTrailingZeros();
}
现在,您原来的 Stream
管道可以工作,但请注意,您使用的 reduce
变体不会 return 和 Optional
(因为您要通过一个标识值),因此您应该更改方法的 return 类型:
public BigDecimal sumOfGeneralTotal(List<PDDocument> document) {
return document.stream().map(this::generalTotalForSingleInvoice).reduce(BigDecimal.ZERO, BigDecimal::add);
}
如何在流函数中将 String 解析为 BigDecimal,我想将 属性 设置为 String,然后将其解析为 BigDecimal,最后通过计算从每个函数中获取的总和来获得总和目的。 (目前在BigDecimal::add上有一条消息无法解析方法添加)谢谢。
public Optional<BigDecimal> generalTotalForSingleInvoice(PDDocument pdDocument) {
return Optional.of(new BigDecimal(findFirst(extractText(pdDocument), INVOICE_GENERAL_TOTAL)
.replaceAll("\.", "")
.replaceAll(",", ".")).stripTrailingZeros());
}
public Optional<BigDecimal> sumOfGeneralTotal(List<PDDocument> document) {
return document.stream().map(this::generalTotalForSingleInvoice).reduce(BigDecimal.ZERO, BigDecimal::add);
}
排除基于generalTotalForSingleInvoice
实现的return类型,你仍然可以使用filter
和map
来确保只有PDDocument
哪个存在有助于在你的减少中积累:
return document.stream()
.map(this::generalTotalForSingleInvoice)
.filter(Optional::isPresent)
.map(Optional::get)
.reduce(BigDecimal::add);
您正在创建一个 Stream<Optional<BigDecimal>>
,并且您不能用 BinaryOperator<BigDecimal>
减少 Stream
,所以 BigDecimal::add
不适合。
因为你的 generalTotalForSingleInvoice
永远不能 return 一个空的 Optional
,它没有理由 return 一个 Optional
。您可以将其更改为:
public BigDecimal generalTotalForSingleInvoice(PDDocument pdDocument) {
return new BigDecimal(findFirst(extractText(pdDocument), INVOICE_GENERAL_TOTAL)
.replaceAll("\.", "")
.replaceAll(",", ".")).stripTrailingZeros();
}
现在,您原来的 Stream
管道可以工作,但请注意,您使用的 reduce
变体不会 return 和 Optional
(因为您要通过一个标识值),因此您应该更改方法的 return 类型:
public BigDecimal sumOfGeneralTotal(List<PDDocument> document) {
return document.stream().map(this::generalTotalForSingleInvoice).reduce(BigDecimal.ZERO, BigDecimal::add);
}