In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Streams API to group and partition Stream in Java 8. I think it is very practical, so I share it with you. I hope you can get something after reading this article.
Grouping
First, we use the employees in the imperative style Java program stream (before the lambda expression) to be grouped by city:
Map result = new HashMap ()
For (Employee e: employees) {
String city = e.getCity ()
List empsInCity = result.get (city)
If (empsInCity = = null) {
EmpsInCity = new ArrayList ()
Result.put (city, empsInCity)
}
EmpsInCity.add (e)
}
You may be familiar with writing such code, and as you can see, such a simple task requires so much code!
In Java 8, you can use the groupingBy collector to accomplish the same function with a single statement, like this:
Map employeesByCity =
Employees.stream () .collect (groupingBy (Employee::getCity))
The result is shown in the following map:
{New York= [Charles], Hong Kong= [Dorothy], London= [Alice, Bob]}
You can also calculate the number of employees in each city by passing a count collector to the groupingBy collector. The role of the second collector is to recursively operate on each element in the same group of flow classifications.
Map numEmployeesByCity =
Employees.stream () .collect (groupingBy (Employee::getCity, counting ()
The result is shown in the following map:
{New York=1, Hong Kong=1, London=2}
By the way, this function is equivalent to the following SQL statement:
Select city, count (*) from Employee group by city
Another example is to calculate the average age of each city, which can be used in conjunction with the averagingInt and groupingBy collectors:
Map avgSalesByCity =
Employees.stream () .collect (groupingBy (Employee::getCity)
AveragingInt (Employee::getNumSales)
The result is as follows: map:
{New York=160.0, Hong Kong=190.0, London=175.0}
Zoning
A partition is a special kind of grouping, and the resulting map contains at least two different groups-- a true and a false. For example, if you want to find the best employees, you can divide all employees into two groups, one with sales greater than N and the other less than N, using the partitioningBy collector:
Map partitioned =
Employees.stream () .collect (partitioningBy (e-> e.getNumSales () > 150))
Output the following results:
{false= [Bob], true= [Alice, Charles, Dorothy]}
You can also pass the groupingBy collector to the partitioningBy collector to partition and group joint use. For example, you can count the number of employees in each city in each district:
Map result =
Employees.stream () .collect (partitioningBy (e-> e.getNumSales () > 150)
GroupingBy (Employee::getCity, counting ()
This generates a secondary Map:
{false= {London=1}, true= {New York=1, Hong Kong=1, London=1}}
The above is how to use Streams API to group and partition Stream in Java 8. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.