In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the Integer type reduction method of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Stream API provides us with Stream.reduce to implement the reduction of collection elements. The reduce function takes three parameters:
Identity identity: an element that is the initial value of the reduction operation and, if the stream is empty, the default result.
Accumulator accumulator: a function with two parameters: the partial result of the reduction operation and the next element of the stream.
Combiner combiner (optional): a function used to merge partial results of a reduction operation when the reduction is parallelized, or when the type of the accumulator parameter does not match the type implemented by the accumulator.
Let's first understand the accumulator:
The phase accumulation result is used as the first parameter of the accumulator
Collection traversal element as the second parameter of the accumulator
Integer type reduction
The reduce initial value is 0, and the accumulator can be either an lambda expression or a method reference.
List numbers = Arrays.asList (1,2,3,4,5,6)
Int result = numbers
.stream ()
.reduce (0, (subtotal, element)-> subtotal + element)
System.out.println (result); / / 21
Int result = numbers
.stream ()
.reduce (0, Integer::sum)
System.out.println (result); / / 21
String type reduction
Not only the Integer type can be reduced, but any type of collection can be reduced as long as the accumulator parameter types match.
List letters = Arrays.asList ("a", "b", "c", "d", "e")
String result = letters
.stream ()
.reduce ("", (partialString, element)-> partialString + element)
System.out.println (result); / / abcdeString result = letters
.stream ()
.reduce ("", String::concat)
System.out.println (result); / / ancde
Complex object reduction
Calculate the total age of all employees.
Employee E1 = new Employee (1cm23, "M", "Rick", "Beethovan")
Employee e2 = new Employee (2Pol 13, "F", "Martina", "Hengis")
Employee E3 = new Employee (32.43, "M", "Ricky", "Martin")
Employee e4 = new Employee (4jue 26, "M", "Jon", "Lowman")
Employee e5 = new Employee (5Magee 19, "F", "Cristine", "Maria")
Employee e6 = new Employee (6, 15, "M", "David", "Feezor")
Employee E7 = new Employee (7, 68, "F", "Melissa", "Roy")
Employee E8 = new Employee (82.79, "M", "Alex", "Gussin")
Employee E9 = new Employee (9, 15, "F", "Neetu", "Singh")
Employee e10 = new Employee (10Jet 45, "M", "Naveen", "Jain"); List employees = Arrays.asList (e1, e2, e3, e4, e5, E6, E7, E8, E9, E10); Integer total = employees.stream (). Map (Employee::getAge). Reduce (0Magol Integerpurpursum)
System.out.println (total);
First use map to process the elements in the Stream stream from Employee type to Integer type (age).
Then reduce the Integer types in the Stream stream
The use of Combiner combiner
In addition to using the map function to implement set reduction after type conversion, we can also use the Combiner combiner, which uses the Combiner combiner for the first time. Because the element in the Stream stream is Employee and the return value of the accumulator is Integer, the types of the two do not match. In this case, the Combiner combiner can be used to second reduce the results of the accumulator, which is equivalent to a type conversion.
Integer total3 = employees.stream ()
.reduce (0, (totalAge,emp)-> totalAge + emp.getAge (), Integer::sum); / / Note that the reduce method has three parameters.
System.out.println (total);
The results are calculated in the same way as data type conversions using map.
Parallel flow data reduction (using combiner)
For the reduction calculation of set elements with a large amount of data, it can better reflect the power of Stream parallel flow computing.
When performing parallel flow calculations, it is possible to divide collection elements into multiple groups. In order to accumulate the results of the grouping calculation more quickly, a combiner can be used.
Integer total2 = employees
.parallelStream ()
.map (Employee::getAge)
.reduce (0MagneIntegerGRAPHER _ sum); / / notice that the reduce method has three parameters
System.out.println (total); / / 346What is the Integer type reduction method of Java? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.