Background: There is a user order query home page (default query last 20 orders at the same time support multi-condition query), orders have a variety of, such as distribution order, buyer order, activity list, etc., now there is a demand for distribution buyer order needs to add a supplier information. The SupplierShopName is obtained by calling the remote interface with the SupplierShopId as an argument.

Development thinking: from the original single use modified to batch call, the store side to the bulk query interface to support the maximum query quantity of 20, for multi-condition query if the pageSize modification needs to be repeatedly called, here need to obtain the distribution of buyers for the number of calculation, so the first need to obtain the number of distribution buyers. Here, the flow operation is used to judge the business criteria and filter out the orders that meet the conditions. Get the list for batch interface call calculation, call the return result into the list set. The object type of the list is a defined BO (with store ID and store alias attributes). The simplest way is to use two for loops that match the store ID in the BO with the store ID of each object in the list into a specific OrderTail. Considering time efficiency, map is used for optimization. Key is the store ID and value is the store name

Error: Incorrect Duplicate key is reported after the Stream operation

stream()
    .collect(
        Collectors.toMap(ShopMetaInfo::getKdtId, ShopMetaInfo::getShopName));
Copy the code

The reason:

Multiple distribution orders may have the same supplier.

Solution:

The Tomap method in Java8 Collectors provides a gateway for converting data into a Map. The Key cannot be repeated when two parameters (Key and Value can be declared directly as shown in the following figure) are applied

In fact, java8 already gives us a way to solve this problem: the third parameter of the method is shown

In fact, java8 already gives us a way to solve this problem: the third parameter of the method is shown

Java8 solution: If two keys are the same, only one key can exist. How to handle the corresponding value? Value does this (we’ll write the method body ourselves, I’ll just concatenate it with a comma)

Refer to the blog