猿问

需要根据父子关系将数据列表转换为列表列表

我需要根据父子关系将数据列表转换为列表列表。如果 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 }]

]

请检查并让我知道我们如何实施。提前致谢


当年话下
浏览 107回答 1
1回答

PIPIONE

为了表示树结构,我使用了ArrayList,其中节点的索引等于其在数组中的索引 + 1。如果您有稀疏树/某些索引可能丢失,请使用映射的等效方法。使用 Java 8 流 API 的解决方案:public static void main( String[] args ) {&nbsp; &nbsp; &nbsp; &nbsp; List<ResponseData> responseDataList = Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 1, -1 ),&nbsp; // changed null to -1 as null can't be a map key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 2, 1 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 3, 1 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 4, 1 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 5, 2 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 6, 2 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 7, 3 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 8, 3 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 9, 4 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 10, 4 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 11, 5 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 12, -1 ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ResponseData( 13, 12 )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; final Map<Integer, List<ResponseData>> map = responseDataList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect( Collectors.groupingBy( o -> getLevel( responseDataList, o, 0 ) ) );&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( map );&nbsp; &nbsp; &nbsp; &nbsp; // To convert the Map to a List of Lists:&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( new ArrayList<>( map.values() ));&nbsp; &nbsp; }&nbsp; &nbsp; private static int getLevel(List<ResponseData> nodes, ResponseData responseData, int level) {&nbsp; &nbsp; &nbsp; &nbsp; if( responseData.parent == -1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return level;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return getLevel( nodes, nodes.get( responseData.parent - 1 ), level + 1 );&nbsp; // -1 to adjust index&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static final class ResponseData {&nbsp; &nbsp; &nbsp; &nbsp; public int id;&nbsp; &nbsp; &nbsp; &nbsp; public int parent;&nbsp; &nbsp; &nbsp; &nbsp; public ResponseData( int id, int parent ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.parent = parent;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return String.format( "{id: %d, parent: %d}", id, parent );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }此外,此代码期望您的树确实是一棵树。如果有任何循环,它将无限循环,最终因堆栈溢出而失败。
随时随地看视频慕课网APP

相关分类

Java
我要回答