This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

🤞 personal home page: @Qingcheng sequence member Stone 🤞 fan benefits: plus one to one fan group to answer questions, get free rich resume template, improve learning materials, do a good job in the new era of volume king!

The above Java8 | how to initialize the Map gracefully & List2Map?” How to initialize a Map and List2Map. There are lots of good suggestions for initializing a Map. In this paper, the Group By processing of set data is likely to be used in the actual development, and various methods are practiced to solve the problem.

First, development issues

Many collections of data grouping are quite common in real development, such as the grouping of product data by type that is passed to the front end. The most common way to do this is to traverse the entire collection and then store collections of different types by determining type constructs. So is there a better way,Java8 groupingBy can help us.

Similarly, suppose we have a class Product, as shown in the following code.

@Getter
@Setter
@ToString
@Builder
class Product{
    private Long id;
    private String category;
    private String name;
    private int count;
}
Copy the code

We now have data in the List format.

static List<Product> getList(a){
    final List<Product> productList = new ArrayList<>(100);
    for(int i =1; i<=100; i++){ productList.add(Product.builder() .id((long) i)
            .name("name"+i)
            .category("category"+i%9)
            .count(i)
            .build());
    }
    return productList;
}
Copy the code

Now let’s do some complicated processing with this data.

Two, the simplest single column processing

Group by category type.

final Map<String, List<Product>> maps = productList.stream()
    .collect(Collectors.groupingBy(Product::getCategory));
Copy the code

For the Collectors. GroupingBy parameter Function
classifier is the Key.

3. Count the total number of specified columns after grouping

Group by category type and count the total count for each type.

Map<String, Integer> maps = productList.stream()
        .collect(Collectors.groupingBy(Product::getCategory, summingInt(Product::getCount)));
Copy the code

Follow the code above to get the sum from the grouping results. Here is the aggregation operation, if you want to do filtering, for example to see the largest number of products under the type group how to do? Use maxBy(comparingInt(*)).

Map<String, Optional<Product>> maps = productList.stream()
        .collect(Collectors.groupingBy(Product::getCategory, maxBy(comparingInt(Product::getCount))));
Copy the code

Using these existing JDK methods to convert data formats in memory can greatly improve development efficiency.


Boy, haven’t you seen enough? Click on the details of the stone, casually have a look, maybe there is a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!