Java: Sum of values inside a Collection of DTO [closed]












2














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);
}









share|improve this 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
















2














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);
}









share|improve this 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














2












2








2







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);
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















6














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();





share|improve this answer























  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














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();





share|improve this answer























  • 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
















6














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();





share|improve this answer























  • 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














6












6








6






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();





share|improve this answer














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();






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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



Popular posts from this blog

Catalogne

Violoncelliste

Héron pourpré