java树结构

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。java树结构,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

package com.ri;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.checkerframework.checker.units.qual.m;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.ImmutableMap;
public class Tre { 
	//JSON.parseObject(jsonStr, new TypeReference<List>() {});  
    public static void main(String[] args) {
    	    //已有树结构转对象
    	    String jsonStr = "[{\"parentID\":0,\"id\":1,\"name\": \"商品一级菜单\",\"type\": \"商品\",\"code\": \"0001\",\"childMenu\":[{\"parentID\":1,\"id\":2,\"name\": \"商品二级菜单\",\"type\": \"商品\",\"code\": \"0001\",\"childMenu\":[{\"parentID\":2,\"id\":3,\"name\": \"商品三级菜单\",\"type\": \"商品\",\"code\": \"0001\",\"childMenu\":[]}]}]}]";
    	    List<MenuItem> menuItems = JSON.parseArray(jsonStr, MenuItem.class);
    	    
    	    
            // 控制展示层级为1级
    	    //menuItems = collectChildMenu(menuItems, 1, 0);
            // 打印结果
    	    //for (MenuItem item : menuItems) {
    	    //    System.out.println(item.getName());
    	    //}

            
             
            //树结构转扁平化对象
            List<MenuItem> flatMenuList = new ArrayList<>();
            flattenMenu(menuItems, flatMenuList);
    	    for (MenuItem item : flatMenuList) {
    	        System.out.println(item);
    	    }

            
            
            //json对象数据列表转树结构
            menuItems = JSON.parseArray("[{\"parentID\":0,\"id\":1,\"name\": \"商品一级菜单\",\"type\": \"商品\",\"code\": \"0001\"},{\"parentID\":1,\"id\":2,\"name\": \"商品二级菜单\",\"type\": \"商品\",\"code\": \"0001\"}]", MenuItem.class);
            Map<Integer, MenuItem> menuMap = menuItems.stream().collect(Collectors.toMap(MenuItem::getId, item -> item));
            List<MenuItem> treeNodes = buildTree(menuMap);
            System.out.println(treeNodes);


            MenuItem menuItem = findMenuItemByName(treeNodes, "商品一级菜单");
            System.out.println(menuItem);
            
	}
    public static void flattenMenu(List<MenuItem> items, List<MenuItem> flatList) {
        for (MenuItem item : items) {
            flatList.add(item);
            if (item.getChildMenu() != null && !item.getChildMenu().isEmpty()) {
                flattenMenu(item.getChildMenu(), flatList);
                //item.getChildMenu().clear();
            }
        }
    } 


    private static List<MenuItem> buildTree(Map<Integer, MenuItem> menuMap) {
        List<MenuItem> treeNodes = new ArrayList<>();
        for (MenuItem item : menuMap.values()) {
            if (item.getParentID() == 0) {
                item.setChildMenu(buildSubTree(menuMap, item));
                treeNodes.add(item);
            }
        }
        return treeNodes;
    }
    
    public static List<MenuItem> collectChildMenu(List<MenuItem> items, int level, int parentId) {
        List<MenuItem> result = new ArrayList<>();
        for (MenuItem item : items) {
            if (item.getParentID() == parentId) {
                result.add(item);
                if (level > 1 && item.getChildMenu() != null) {
                    List<MenuItem> children = collectChildMenu(item.getChildMenu(), level - 1, item.getId());
                    if (!children.isEmpty()) {
                        item.setChildMenu(children);
                    }
                } else {
                    item.setChildMenu(null); // 如果不需要展示下级菜单,则清空
                }
            }
        }
        return result;
    }

    private static List<MenuItem> buildSubTree(Map<Integer, MenuItem> menuMap, MenuItem parentNode) {
        List<MenuItem> children = new ArrayList<>();
        for (MenuItem item : menuMap.values()) {
            if (item.getParentID() == parentNode.getId()) {
                item.setChildMenu(buildSubTree(menuMap, item));
                children.add(item);
            }
        }
        parentNode.setChildMenu(children);
        return parentNode.getChildMenu();
    }
	private static MenuItem findMenuItemByName(List<MenuItem> menuList, String targetName) {
	        for (MenuItem item : menuList) {
	            if (item.getName().equals(targetName)) {
	                item.getChildMenu().clear();
	                return item;
	            }
	            if (item.getChildMenu() != null && !item.getChildMenu().isEmpty()) {
	                MenuItem foundItem = findMenuItemByName(item.getChildMenu(), targetName);
	                if (foundItem != null) {
	                    return foundItem;
	                }
	            }
	        }
	        return null; // 如果未找到匹配项,则返回null
	    }
	
	}
	 class MenuItem {
    private String name;
    private String type;
    private String code;
    private int id;
    private int parentID;
    private List<MenuItem> childMenu;
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}
	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the parentID
	 */
	public int getParentID() {
		return parentID;
	}
	/**
	 * @param parentID the parentID to set
	 */
	public void setParentID(int parentID) {
		this.parentID = parentID;
	}
	/**
	 * @return the childMenu
	 */
	public List<MenuItem> getChildMenu() {
		return childMenu;
	}
	/**
	 * @param childMenu the childMenu to set
	 */
	public void setChildMenu(List<MenuItem> childMenu) {
		this.childMenu = childMenu;
	}

    
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/200894.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!