最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java list如何根據(jù)id獲取子節(jié)點(diǎn)

 更新時(shí)間:2020年03月21日 11:29:20   作者:學(xué)無終  
這篇文章主要介紹了Java list如何根據(jù)id獲取子節(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

工作中因業(yè)務(wù)需求,將數(shù)據(jù)庫中的樹狀結(jié)構(gòu)的數(shù)據(jù)根據(jù)父節(jié)點(diǎn)獲取所有的子節(jié)點(diǎn)

實(shí)現(xiàn)思路

1.獲取整個(gè)數(shù)據(jù)的list集合數(shù)據(jù)

2.將數(shù)據(jù)分組,java8 list有g(shù)roupby分組,java8之前的自己遍歷整理

3.分組后遞歸獲取子節(jié)點(diǎn),有子節(jié)點(diǎn)的添加,沒有的設(shè)置子節(jié)點(diǎn)并刪除分組的數(shù)據(jù),知道分組數(shù)據(jù)刪完

Tree.java

@Data
public class Tree {
  private Integer id;
  private Integer pId;
  private String key;
  private String value;
  private List<Tree> childList;
}

TreeUtils.java

public class TreeUtils {
  static List<Tree> trees ;
  static {
    String jsonStr = "[" +
        "{\"id\":100,\"pId\":1,\"key\":\"root\", \"value\": \"root\"}," +
        "{\"id\":1000,\"pId\":100,\"key\":\"node1\", \"value\": \"node1\"}," +
        "{\"id\":2000,\"pId\":100,\"key\":\"node2\",\"value\": \"node2\"}," +
        "{\"id\":3000,\"pId\":100,\"key\":\"node3\",\"value\": \"node3\"}," +
        "{\"id\":1100,\"pId\":1000,\"key\":\"node11\",\"value\": \"node11\"}," +
        "{\"id\":1200,\"pId\":1000,\"key\":\"node12\",\"value\": \"node12\"}," +
        "{\"id\":1110,\"pId\":1100,\"key\":\"node111\",\"value\": \"node111\"}," +
        "{\"id\":1120,\"pId\":1100,\"key\":\"node112\",\"value\": \"node112\"}," +
        "{\"id\":2100,\"pId\":2000,\"key\":\"node21\",\"value\": \"node21\"}," +
        "{\"id\":2200,\"pId\":2000,\"key\":\"node22\",\"value\": \"node22\"}," +
        "{\"id\":2110,\"pId\":2100,\"key\":\"node211\",\"value\": \"node21\"}" +
        "]";
    trees = JSONObject.parseArray(jsonStr, Tree.class);
  }

  public static void main(String[] args) {
    Tree tree = metaTree(trees, 100);
    /**
     * Tree@6073f712[id=100,pId=1,key=root,value=root,childList=[
     *           Tree(id=1000, pId=100, key=node1, value=node1, childList=[
     *             Tree(id=1100, pId=1000, key=node11, value=node11, childList=[
     *               Tree(id=1110, pId=1100, key=node111, value=node111, childList=null),
     *               Tree(id=1120, pId=1100, key=node112, value=node112, childList=null)]),
     *             Tree(id=1200, pId=1000, key=node12, value=node12, childList=null)]),
     *           Tree(id=2000, pId=100, key=node2, value=node2, childList=[
     *             Tree(id=2100, pId=2000, key=node21, value=node21, childList=[
     *               Tree(id=2110, pId=2100, key=node211, value=node21, childList=null)]),
     *               Tree(id=2200, pId=2000, key=node22, value=node22, childList=null)]),
     *           Tree(id=3000, pId=100, key=node3, value=node3, childList=null)]]
     */
    System.out.println("tree:" + ToStringBuilder.reflectionToString(tree));
  }

  private static Tree metaTree(List<Tree> treeList, Integer id) {
//此處getId getPId根據(jù)自己實(shí)際情況更改
    Tree treeConfig = treeList.stream().filter(tree -> tree.getId().equals(id)).collect(Collectors.toList()).get(0);
    Map<Integer, List<Tree>> collect = treeList.stream().filter(type -> type.getPId() != null).collect(Collectors.groupingBy(Tree::getPId));
    if (collect != null && collect.size() > 0) {
      recursion(collect, treeConfig);
    }
    return treeConfig;
  }

  private static Tree recursion(Map<Integer, List<Tree>> maps, Tree tree) {
    if (tree.getChildList() == null) {
      if (maps.get(tree.getId()) != null) {
        tree.setChildList(maps.get(tree.getId()));
        maps.remove(tree.getId());
        if (maps.size() > 0) {
          recursion(maps, tree);
        }
      }
    } else {
      List<Tree> metaTypeList = tree.getChildList();
      if (metaTypeList != null && metaTypeList.size() > 0) {
        for (Tree meta : metaTypeList) {
          recursion(maps, meta);
        }
      }
    }
    return tree;
  }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 快速解決跨域請(qǐng)求問題:jsonp和CORS

    快速解決跨域請(qǐng)求問題:jsonp和CORS

    這篇文章主要介紹了快速解決跨域請(qǐng)求問題:jsonp和CORS,涉及jsonp和CORS的介紹,分享了前端 jQuery 寫法,后端 SpringMVC 配置,后端非 SpringMVC 配置等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • 解析SpringSecurity自定義登錄驗(yàn)證成功與失敗的結(jié)果處理問題

    解析SpringSecurity自定義登錄驗(yàn)證成功與失敗的結(jié)果處理問題

    這篇文章主要介紹了SpringSecurity系列之自定義登錄驗(yàn)證成功與失敗的結(jié)果處理問題,本文通過實(shí)例給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Jmeter安裝的方法步驟詳解

    Jmeter安裝的方法步驟詳解

    這篇文章主要介紹了Jmeter安裝的方法步驟詳解,Apache JMeter是Apache組織開發(fā)的基于Java的壓力測試工具。用于對(duì)軟件做壓力測試,它最初被設(shè)計(jì)用于Web應(yīng)用測試,但后來擴(kuò)展到其他測試領(lǐng)域,需要的朋友可以參考下
    2019-07-07
  • SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

    SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

    這篇文章主要介紹了SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • java實(shí)現(xiàn)的計(jì)算器功能示例【基于swing組件】

    java實(shí)現(xiàn)的計(jì)算器功能示例【基于swing組件】

    這篇文章主要介紹了java實(shí)現(xiàn)的計(jì)算器功能,結(jié)合實(shí)例形式分析了java基于swing組件實(shí)現(xiàn)計(jì)算器功能相關(guān)運(yùn)算操作技巧,需要的朋友可以參考下
    2017-12-12
  • Java基礎(chǔ)之序列化與反序列化詳解

    Java基礎(chǔ)之序列化與反序列化詳解

    這篇文章主要介紹了Java基礎(chǔ)之序列化與反序列化詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • Mybatis中isNotNull與isNotEmpty的使用心得

    Mybatis中isNotNull與isNotEmpty的使用心得

    這篇文章主要介紹了Mybatis中isNotNull與isNotEmpty的使用心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring中的@PropertySource注解源碼詳解

    Spring中的@PropertySource注解源碼詳解

    這篇文章主要介紹了Spring中的@PropertySource注解源碼詳解,@PropertySource注解用于指定資源文件讀取的位置,它不僅能讀取properties文件,也能讀取xml文件,并且通過yaml解析器,配合自定義PropertySourceFactory實(shí)現(xiàn)解析yaml文件,需要的朋友可以參考下
    2023-11-11
  • Java中常見死鎖與活鎖的實(shí)例詳解

    Java中常見死鎖與活鎖的實(shí)例詳解

    這篇文章主要介紹了Java中常見死鎖與活鎖的實(shí)例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例

    java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例

    這篇文章主要介紹了 java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評(píng)論

白沙| 高阳县| 孟津县| 延庆县| 商丘市| 大同市| 乌拉特前旗| 长沙市| 西乌| 龙南县| 沧州市| 棋牌| 仁怀市| 资阳市| 龙川县| 太和县| 奇台县| 广汉市| 信阳市| 东港市| 万盛区| 龙里县| 汤原县| 砚山县| 体育| 福鼎市| 涪陵区| 青海省| 包头市| 泸州市| 长子县| 巢湖市| 乡城县| 元朗区| 高平市| 东海县| 会昌县| 资兴市| 肇州县| 彝良县| 石柱|