查询数据转换为树结构

文章目录

    • 方法一:通用法
    • 方法二: java8 Stream()

方法一:通用法

将查询列表循环遍历,有子级的挂上子级,没有子级的单独成顶级

 public static List<TSysMenu> toMenuTreeList(List oldList) throws Exception {
        String prePid = "-1";
        List<Map<String,Object>> tmpList = new ArrayList<>();
        Map<String, List<Map<String,Object>>> childMap = new HashMap<>();
        Map<String, Map<String,Object>> allMap = new HashMap<>();
        for (Object obj : oldList) {
            Map<String,Object> menuJSON = MapperUtils.json2mapDeeply(MapperUtils.obj2json(obj));
            if (!menuJSON.get("pid").equals(prePid ) && !"-1".equals(prePid)) {
                if (allMap.containsKey(prePid) && !allMap.get(prePid).containsKey("children")) {
                    allMap.get(prePid).put("children", tmpList);
                } else {
                    childMap.put(prePid, tmpList);
                }
                tmpList = new ArrayList<>();
            }
            if (childMap.containsKey(menuJSON.get("id"))) {
                //菜单有子菜单,安装上
                menuJSON.put("children", childMap.get(menuJSON.get("id")));
                childMap.remove(menuJSON.get("id"));
            }
            prePid = (String) menuJSON.get("pid");
            tmpList.add(menuJSON);
            allMap.put((String) menuJSON.get("id"), menuJSON);
        }

        if(childMap.size() > 0){
            for(List<Map<String,Object>> child : childMap.values()){
                tmpList.addAll(child);
            }

        }
        List<TSysMenu> tSysMenus = MapperUtils.json2list(MapperUtils.obj2json(tmpList), TSysMenu.class);
        return tSysMenus;
    }

MapperUtils 类:



import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MapperUtils {
    private final static ObjectMapper objectMapper = new ObjectMapper();

    static {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        objectMapper.registerModule(javaTimeModule);
    }

    /**
     * 转换为 JSON 字符串
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static String obj2json(Object obj) throws Exception {
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        return objectMapper.writeValueAsString(obj);
    }

    /**
     * 转换为 JSON 字符串,忽略空值
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static String obj2jsonIgnoreNull(Object obj) throws Exception {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper.writeValueAsString(obj);
    }

    /**
     * 转换为 JavaBean
     *
     * @param jsonString
     * @param clazz
     * @return
     * @throws Exception
     */
    public static <T> T json2pojo(String jsonString, Class<T> clazz) throws Exception {
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return objectMapper.readValue(jsonString, clazz);
    }

    /**
     * 字符串转换为 Map
     *
     * @param jsonString
     * @return
     * @throws Exception
     */
    public static <T> Map<String, Object> json2map(String jsonString) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.readValue(jsonString, Map.class);
    }

    /**
     * 字符串转换为 Map
     */
    public static <T> Map<String, T> json2map(String jsonString, Class<T> clazz) throws Exception {
        Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) objectMapper.readValue(jsonString, new TypeReference<Map<String, T>>() {
        });
        Map<String, T> result = new HashMap<String, T>();
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
        }
        return result;
    }

    /**
     * 深度转换 JSON 成 Map
     *
     * @param json
     * @return
     */
    public static Map<String, Object> json2mapDeeply(String json) throws Exception {
        return json2MapRecursion(json, objectMapper);
    }

    /**
     * 把 JSON 解析成 List,如果 List 内部的元素存在 jsonString,继续解析
     *
     * @param json
     * @param mapper 解析工具
     * @return
     * @throws Exception
     */
    private static List<Object> json2ListRecursion(String json, ObjectMapper mapper) throws Exception {
        if (json == null) {
            return null;
        }

        List<Object> list = mapper.readValue(json, List.class);

        for (Object obj : list) {
            if (obj != null && obj instanceof String) {
                String str = (String) obj;
                if (str.startsWith("[")) {
                    obj = json2ListRecursion(str, mapper);
                } else if (obj.toString().startsWith("{")) {
                    obj = json2MapRecursion(str, mapper);
                }
            }
        }

        return list;
    }

    /**
     * 把 JSON 解析成 Map,如果 Map 内部的 Value 存在 jsonString,继续解析
     *
     * @param json
     * @param mapper
     * @return
     * @throws Exception
     */
    private static Map<String, Object> json2MapRecursion(String json, ObjectMapper mapper) throws Exception {
        if (json == null) {
            return null;
        }

        Map<String, Object> map = mapper.readValue(json, Map.class);

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object obj = entry.getValue();
            if (obj != null && obj instanceof String) {
                String str = ((String) obj);

                if (str.startsWith("[")) {
                    List<?> list = json2ListRecursion(str, mapper);
                    map.put(entry.getKey(), list);
                } else if (str.startsWith("{")) {
                    Map<String, Object> mapRecursion = json2MapRecursion(str, mapper);
                    map.put(entry.getKey(), mapRecursion);
                }
            }
        }

        return map;
    }

    /**
     * 将 JSON 数组转换为集合
     *
     * @param jsonArrayStr
     * @param clazz
     * @return
     * @throws Exception
     */
    public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception {
        JavaType javaType = getCollectionType(ArrayList.class, clazz);
        List<T> list = objectMapper.readValue(jsonArrayStr, javaType);
        return list;
    }


    /**
     * 获取泛型的 Collection Type
     *
     * @param collectionClass 泛型的Collection
     * @param elementClasses  元素类
     * @return JavaType Java类型
     * @since 1.0
     */
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    /**
     * 将 Map 转换为 JavaBean
     *
     * @param map
     * @param clazz
     * @return
     */
    public static <T> T map2pojo(Map map, Class<T> clazz) {
        return objectMapper.convertValue(map, clazz);
    }

    /**
     * 将 Map 转换为 JSON
     *
     * @param map
     * @return
     */
    public static String mapToJson(Map map) {
        try {
            return objectMapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将 JSON 对象转换为 JavaBean
     *
     * @param obj
     * @param clazz
     * @return
     */
    public static <T> T obj2pojo(Object obj, Class<T> clazz) {
        return objectMapper.convertValue(obj, clazz);
    }

}

方法二: java8 Stream()

  1. 首先筛选父id =0 的集合,并设置他们的子集
  2. 创建 设置子菜单递归方法,筛选父级id = 当前菜单id 的集合,再执行设置子菜单递归方法。
@Override
    public List<SysMenuVO> buildMenuTree(List<SysMenuVO> menuVOList) {
        // 判断 menuVOList 里面parentId 是否包含 0 ,如果不包含则反向查询父菜单并添加到 menuVOList
        Set<Long> menuIds = menuVOList.stream().map(SysMenuVO::getMenuId).collect(Collectors.toSet());
        Set<Long> parentIds = menuVOList.stream().map(SysMenuVO::getParentId).collect(Collectors.toSet());
        if(!parentIds.contains(new Long(0))){
            if (!CollectionUtils.isEmpty(menuVOList)){
                Set<SysMenuVO> parentCollectSet = new HashSet<>();
                for (SysMenuVO sysMenuVO : menuVOList){
                    SysMenuVO parentMenuVO = this.sysMenuMapper.getSysMenuVOByMenuId(sysMenuVO.getParentId());
                    if (parentMenuVO !=null &&!menuIds.contains(parentMenuVO.getMenuId())){
                        parentCollectSet.add(parentMenuVO);
                    }
                }
                menuVOList.addAll(parentCollectSet);
            }

        }

        List<SysMenuVO> sysMenuVOList = menuVOList.stream()
                .filter(item -> item.getParentId().longValue() == 0L)
                .map(menuVO -> {
                    menuVO.setChildren(setMenuChildren(menuVO, menuVOList));
                    return menuVO;
                })
                .collect(Collectors.toList());

        return sysMenuVOList;
    }

    private List<SysMenuVO> setMenuChildren(SysMenuVO menuVO, List<SysMenuVO> menuVOList) {
        List<SysMenuVO> list = menuVOList.stream()
                .filter(f -> f.getParentId().longValue() == menuVO.getMenuId().longValue())
                .map(item -> {
                    item.setChildren(setMenuChildren(item, menuVOList));
                    return item;
                })
                .collect(Collectors.toList());

        return list;
    }

你可能感兴趣的:(java,基础,windows,python,linux)