Use of Stream of three-level classification

reference resources

brief introduction

When I encountered this three-level classification problem in learning grain mall with Shang Silicon Valley, I used Stream Api to record it


The main content is the three-level classification of the commodity category table. The category table is designed as follows. It mainly focuses on the fields of category id, parent category id and sorting

There are only 30 data contents posted here, a total of 1000, and then displayed on the web

After understanding the requirements, CategoryEntity is as follows. Pay attention to the newly added non database field attribute list < CategoryEntity > children, which is used to store the sub categories under this category

public class CategoryEntity implements Serializable {
    private static final long serialVersionUID = 1L;

     * Classification id
    private Long catId;
     * Classification name
    private String name;
     * Parent category id
    private Long parentCid;
     * Hierarchy
    private Integer catLevel;
     * Whether to display [0-do not display, 1 display]
    private Integer showStatus;
     * sort
    private Integer sort;
     * Icon address
    private String icon;
     * Unit of measure
    private String productUnit;
     * Quantity of goods
    private Integer productCount;
     * Sub classification
    @TableField(exist = false)
    private List<CategoryEntity> children;


We focus on the business layer. The dao layer uses mybatis plus. I won't introduce it more

It is here that we use Stream to process all the classification information found from the database


  • Query all classification information from the database list < categoryentity >
  • First, check all the primary classifications (if parentCid is 0, you can also see from the above data content)
  • Then, from these parent categories, find the child categories that belong to them (through the getChildren function)
  • Sorted is sorted in ascending order according to the sort field
  • collect to form a collection
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    public List<CategoryEntity> listWithTree() {
        // 1. Find all categories
        List<CategoryEntity> entities = baseMapper.selectList(null);
        // 2. Assembly tree structure

        // 2.1 primary classification
        List<CategoryEntity> level1Menu = ->
                categoryEntity.getParentCid() == 0
        ).peek((menu) ->
                menu.setChildren(getChildren(menu, entities))
        return level1Menu;

     * Recursively find submenus of all menus
     * @param root
     * @param all
     * @return
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = ->
        ).peek((categoryEntity ->
                categoryEntity.setChildren(getChildren(categoryEntity, all)))
        return children;

Finally, the web is displayed as follows. The JSONView plug-in I use can also view relevant information with F12

The result is what we expected. It is found that the ranking is correct, and "mobile phone" ranks first


If the business does not use Stream, how to do it will be complex. From here, we can understand the power of Stream

What knowledge is needed to use Stream?

First, as the key upgrade of JDK1.8, the lambda expression, functional interface and chain programming of JDK1.8 are the key points

Four functional interfaces

You can directly search the source code and see how the interface is designed to understand how to use it. Let me summarize it in vernacular

Function functional interface

Pass in a parameter that returns the value

Predicate assertion interface

Pass in a parameter, judge by this parameter, and return a Boolean value

Consumer consumer interface

Pass in a parameter, which will be processed without return

Supplier supply interface

No parameter, return a value


In fact, using Stream is to pass in some functional interfaces, and it becomes very easy to use lambda expressions

Think about it, but the experience of using it is not so great. Until you meet the real business, your feeling will be different immediately


Since then, I can finally experience some business scenarios of algorithm problems, and the foundation of Java se is really important. I have to feel more slowly

Tags: Java Spring Boot Back-end

Posted on Fri, 19 Nov 2021 18:50:10 -0500 by mr_tron