将嵌套的地图转换为嵌套的列表。

huangapple go评论71阅读模式
英文:

Convert nested map to nested list

问题

假设我有一个嵌套的 Levels 类,即:

public class Level {
    private Map<String, Level> childLevels;
}

我想将这个类转换为如下所示:

public class ListLevel {
    private List<ListLevel> childLevels;
}

ChildLevels 可能没有被实例化,并且可能为 null。我从一个 Map<String, Level> 开始,希望通过丢弃每个 Map 条目的键来将其转换为 List<ListLevel>

我尝试了下面的代码,但还不太行。编辑:下面的代码在使用新的 ListLevel 类时行不通。

private static List<Level> convertToList(Map<String, Level> levelMap) {
    return levelMap.values().stream()
        .filter(i -> i.getChildLevels() != null)
        .flatMap(i -> Stream.concat(Stream.of(i), convertToList(i.getChildLevels()).stream()))
        .collect(Collectors.toList());
}

有什么建议吗?

编辑:为了澄清,我想将所有嵌套的映射转换为列表,但保持相同的嵌套结构。

英文:

Assume I have a class Level with nested Levels i.e.

public class Level {
    private Map&lt;String, Level&gt; childLevels
}

I want to convert this class into the following:

public class ListLevel {
    private List&lt;ListLevel&gt; childLevels
}

ChildLevels are not necessarily instantiated and may be null. I start with a Map&lt;String, Level&gt; and I want to convert this into a List&lt;ListLevel&gt; by discarding the key of every Map entry.

I've tried the code below but it's not quite there. edit: below code won't work with a new ListLevel class

private static List&lt;Level&gt; convertToList(Map&lt;String, Level&gt; levelMap){
    return levelMap.values().stream()
        .filter(i -&gt; i.getChildLevels() != null)
        .flatMap(i -&gt; Stream.concat(Stream.of(i), convertToList(i.getChildLevels()).stream()))
        .collect(Collectors.toList());
}

Any suggestions?

edit: to clarify, I want to convert all nested maps to lists but maintain the same nested structure

答案1

得分: 2

你需要使用Mapvalues()方法,就像levelMap.values().stream()一样,因为你想要获取映射的值。

private static List<Level> convertToList(Map<String, Level> levelMap){
    return levelMap.values().stream()
        .filter(i -> i.getChildLevels() != null)
        .flatMap(i -> Stream.concat(Stream.of(i), convertToList(i.getChildLevels()).stream()))
        .collect(Collectors.toList());
}

在问题更新后:

private static List<ListLevel> convertToList(Map<String, Level> levelMap){
    if(levelMap == null) return null;
    return levelMap.values().stream()
        .map(i -> convertLevelToListLevel(i, convertToList(i.getChildLevels())))
        .collect(Collectors.toList());
}

这里的ListLevel convertLevelToListLevel(Level level, List<ListLevel> childListLevels)是将Level转换为ListLevel的转换器,正如在评论中提到的。

英文:

You need to use values() of Map like levelMap.values().stream() since you want the values of map.

private static List&lt;Level&gt; convertToList(Map&lt;String, Level&gt; levelMap){
    return levelMap.values().stream()
        .filter(i -&gt; i.getChildLevels() != null)
        .flatMap(i -&gt; Stream.concat(Stream.of(i), convertToList(i.getChildLevels()).stream()))
        .collect(Collectors.toList());
}

After question Updated:

private static List&lt;ListLevel&gt; convertToList(Map&lt;String, Level&gt; levelMap){
    if(levelMap == null) return null;
    return levelMap.values().stream()
        .map(i -&gt; convertLevelToListLevel(i, convertToList(i.getChildLevels())))
        .collect(Collectors.toList());
}

Here ListLevel convertLevelToListLevel(Level level, List&lt;ListLevel&gt; childListLevels) is converter for Level to ListLevel conversion as mention in comment OP already have.

huangapple
  • 本文由 发表于 2020年8月28日 17:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63630903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定