亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要根據父子關系將數據列表轉換為列表列表

需要根據父子關系將數據列表轉換為列表列表

當年話下 2023-04-13 15:20:34
我需要根據父子關系將數據列表轉換為列表列表。如果 parent 為 null 落入一級,則二級將基于一級 id。我的數據如下所示:[    {id:1, parent: null },    {id:2, parent: 1 },    {id:3, parent: 1 },    {id:4, parent: 1 },    {id:5, parent: 2 },    {id:6, parent: 2 },    {id:7, parent: 3 },    {id:8, parent: 3 },    {id:9, parent: 4 },    {id:10, parent: 4 },    {id:11, parent: 5 },    {id:12, parent: null },    {id:13, parent: 12 },]我的代碼是:響應數據Map<String,Map<String,ResponseData>> map = new HashMap<>();for (ResponseData responseData : responseDataList) {    Map<String,responseData> responseDatasMap =  map.get(responseData.getParent());    if(responseDatasMap != null) {        responseDatasMap.put(responseData.getId(),responseData);        map.put(responseData.getParent(),responseDatasMap);    } else {        responseDatasMap =  new HashMap<>();        responseDatasMap.put(responseData.getParent(),responseData);        map.put(responseData.getParent(),responseDatasMap);    }}上面的地圖將包含父級作為鍵和映射到父級的值的地圖List<List<ResponseData>> sections = new ArrayList<>();for (Map.Entry<String,Map<String, ResponseData>> responseDataMap : map.entrySet()) {    Map<String, ResponseData> valueMap = responseDataMap.getValue();    responseDataList = new ArrayList<>();    for(Map.Entry<String, ResponseData> responseData :valueMap.entrySet()) {        responseDataList.add(responseData.getValue());    }    sections.add(responseDataList);}我的輸出如下所示:[    [ {id:1, parent: null } ],    [ {id:2, parent: 1 },{id:3, parent: 1 },{id:4, parent: 1 } ],    [ {id:5, parent: 2 },{id:6, parent: 2 } ],    [ {id:7, parent: 3 },{id:8, parent: 3 } ],    [ {id:9, parent: 4 },{id:10, parent: 4 } ],    [ {id:11, parent: 5 }]]請檢查并讓我知道我們如何實施。提前致謝
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

為了表示樹結構,我使用了ArrayList,其中節點的索引等于其在數組中的索引 + 1。如果您有稀疏樹/某些索引可能丟失,請使用映射的等效方法。


使用 Java 8 流 API 的解決方案:


public static void main( String[] args ) {

        List<ResponseData> responseDataList = Arrays.asList(

            new ResponseData( 1, -1 ),  // changed null to -1 as null can't be a map key

            new ResponseData( 2, 1 ),

            new ResponseData( 3, 1 ),

            new ResponseData( 4, 1 ),

            new ResponseData( 5, 2 ),

            new ResponseData( 6, 2 ),

            new ResponseData( 7, 3 ),

            new ResponseData( 8, 3 ),

            new ResponseData( 9, 4 ),

            new ResponseData( 10, 4 ),

            new ResponseData( 11, 5 ),

            new ResponseData( 12, -1 ),

            new ResponseData( 13, 12 )

        );

        final Map<Integer, List<ResponseData>> map = responseDataList.stream()

                .collect( Collectors.groupingBy( o -> getLevel( responseDataList, o, 0 ) ) );

        System.out.println( map );

        // To convert the Map to a List of Lists:

        System.out.println( new ArrayList<>( map.values() ));

    }


    private static int getLevel(List<ResponseData> nodes, ResponseData responseData, int level) {

        if( responseData.parent == -1 ) {

            return level;

        } else {

            return getLevel( nodes, nodes.get( responseData.parent - 1 ), level + 1 );  // -1 to adjust index

        }

    }


    private static final class ResponseData {

        public int id;

        public int parent;


        public ResponseData( int id, int parent ) {

            this.id = id;

            this.parent = parent;

        }


        @Override

        public String toString() {

            return String.format( "{id: %d, parent: %d}", id, parent );

        }

    }

此外,此代碼期望您的樹確實是一棵樹。如果有任何循環,它將無限循環,最終因堆棧溢出而失敗。


查看完整回答
反對 回復 2023-04-13
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號