Java: Sum of values inside a Collection of DTO [closed]
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
java java-8 sum java-stream
closed as unclear what you're asking by Federico Peralta Schaffner, Holger, Pino, Jesús López, DB5 Nov 29 '18 at 19:25
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
java java-8 sum java-stream
closed as unclear what you're asking by Federico Peralta Schaffner, Holger, Pino, Jesús López, DB5 Nov 29 '18 at 19:25
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
What is your question?
– Holger
Nov 23 '18 at 7:28
add a comment |
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
java java-8 sum java-stream
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
java java-8 sum java-stream
java java-8 sum java-stream
edited Nov 23 '18 at 7:17
nullpointer
43k1091177
43k1091177
asked Nov 23 '18 at 7:13
vinit
164
164
closed as unclear what you're asking by Federico Peralta Schaffner, Holger, Pino, Jesús López, DB5 Nov 29 '18 at 19:25
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Federico Peralta Schaffner, Holger, Pino, Jesús López, DB5 Nov 29 '18 at 19:25
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
What is your question?
– Holger
Nov 23 '18 at 7:28
add a comment |
3
What is your question?
– Holger
Nov 23 '18 at 7:28
3
3
What is your question?
– Holger
Nov 23 '18 at 7:28
What is your question?
– Holger
Nov 23 '18 at 7:28
add a comment |
1 Answer
1
active
oldest
votes
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
add a comment |
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
add a comment |
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
edited Nov 23 '18 at 10:03
answered Nov 23 '18 at 7:15
nullpointer
43k1091177
43k1091177
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
add a comment |
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
Thanks for the solution, just an additional null check. return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
– vinit
Nov 23 '18 at 9:54
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
@vinit yes that would be helpful id your objects could possibly be null.
– nullpointer
Nov 23 '18 at 10:02
add a comment |
3
What is your question?
– Holger
Nov 23 '18 at 7:28