In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article Xiaobian introduces in detail for you "java tree structure stream tool class how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this "java tree structure stream tool class how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
Menu entity class
Package com.example.demo.entity; import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;import java.util.List;@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic class Menu {/ * id * / public Integer id; * name public String name; * parent id, root node is 0 public Integer parentId; * child node information public List childList Public Menu (Integer id, String name, Integer parentId) {this.id = id; this.name = name; this.parentId = parentId;}}
Spanning tree structure utility class
Package com.example.demo.util; import cn.hutool.json.JSONUtil;import com.example.demo.entity.Menu;import java.util.Arrays;import java.util.List;import java.util.Objects;import java.util.stream.Collectors Public class TreeUtil {/ * Recursive query child node * * @ param root root node * @ param all all nodes * @ return root node information * / public static List getChildrens (Menu root, List all) {List children = all.stream () .filter (m-> {return Objects.equals (m.getParentId (), root.getId () }) .map ((m)-> {m.setChildList (getChildrens (m, all)); return m;}) .return children (Collectors.toList ()) } * @ param parentId parent node id * @ param menus menu collection * @ return returns the descendant node tree structure with id equal to parentId (excluding nodes with id equal to parentId) public static List getTree (Integer parentId, List menus) {/ / get parent node List tree = menus.stream () .filter (m-> Objects.equals (parentId) M.getParentId ()) .map (m.setChildList (getChildrens (m, menus)) Return tree; public static void main (String... Args) {/ / simulate queries from the database List menus = Arrays.asList (new Menu (1, "0-1", 0), new Menu (2, "0-1-2", 1), new Menu (3, "0-1-2-3", 2), new Menu (4, "0-1-2-4") ), new Menu (5, "0-1-2-5", 2), new Menu (6, "0-6", 0), new Menu (7, "0-6-7", 6), new Menu (8, "0-6-8", 6), new Menu (9, "0-6-7-9", 7) New Menu (10, "0-6-7-10", 7), new Menu (11, "0-11", 0), new Menu (12, "0-11-12", 11)) List tree = getTree (1menus); System.out.println ("- convert json output result -"); System.out.println (JSONUtil.parseArray (tree) .toString ()) } after reading this, the article "how to use the java Tree structure stream tool Class" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.