Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to Sum the reduce () method in java8's stream flow

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to sum the reduce () method in the stream stream of java8". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to sum the reduce () method in the stream stream of java8" can help you solve the problem.

1.stream (). Reduce () single field summation

(1) Sum of ordinary numbers

Public static void test2 () {List list= Arrays.asList (new Integer [] {1 sum 2, 3 4, 5 5, 7 7, 8 9}); Integer sum=list.stream (). Reduce ((x recorder y)-> x recorder). Get (); System.out.println (sum);}

(2) BigDecimal summation

Public static void main (String [] args) {List list=new ArrayList (); User user1=new User (); user1.setNum1 (new BigDecimal); user1.setNum2 (new BigDecimal); list.add (user1); User user2=new User (); user2.setNum1 (new BigDecimal (100)); user2.setNum2 (new BigDecimal (100)); list.add (user2) BigDecimal sum=list.stream (). Map (User::getNum1). Reduce (BigDecimal::add). Get (); System.out.println (sum);}

However, if there is no data in the list, list.size () = = 0, there will be an error, so you need to modify the code as follows:

BigDecimal sum=list.stream () .map (User::getNum1) / / returns the list of num1. Reduce (BigDecimal.ZERO,BigDecimal::add); / / summation of list fields

0 is returned by default when there is no element in list.

2.stream (). Reduce () multifield summation public static void main (String [] args) {List list=new ArrayList (); User user1=new User (); user1.setNum1 (new BigDecimal (123)); user1.setNum2 (new BigDecimal (100); list.add (user1); User user2=new User (); user2.setNum1 (new BigDecimal (100)); user2.setNum2 (new BigDecimal (100)) List.add (user2); User u=list.stream (). Reduce ((xQuery)-> {User user=new User (); user.setNum1 (x.getNum1 (). Add (y.getNum1 (); user.setNum2 (x.getNum2 (). Add (y.getNum2 ()); return user;}) .get () System.out.println (u.getNum1 () + "-" + u.getNum2 ();}

Similarly, if there are no elements in the list, use the following:

User u=list.stream () .reduce (new User (), (xPowery)-> {User user=new User (); user.setNum1 (x.getNum1 () .add (y.getNum1 (); user.setNum2 (x.getNum2 () .add (y.getNum2 (); return user;})

If you default to a User object, there will be no error.

This is the end of the introduction to "how to sum the reduce () method in java8's stream stream". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report