java menu tables in the database content into a navigation tree

Fields in the database:

menuId   parentMenuId    menuName   link等

First, we must be all the menus found in the database from a List, the following we need to deal with this array, this list will be converted into a tree format

When the original process is very troublesome, it is necessary to do recursive operations, similar to this line of thinking

1, find the root menu

2, according to the id of the root menu to find the entire array parentId is the object id of, put in list

3, the submenu for the second operation is the root assembly, known so far can not find submenu

In this case each and every time you need to find from the entire array, regardless of whether or not one has been treated up.

 

Improved point, jdk8 grouping operation can be done by the method of a list

stuList.stream().collect(Collectors.groupingBy(Student::getStuName));

The method of the object list in list of object types packet

By this method can be converted to a list of key packet field value is assigned to the set of objects of the type of list map

At this time you can recursively up, directly to the plug entered the list, a lot less code.

Guess you like

Origin www.cnblogs.com/heibo/p/12192365.html