英文:
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<String, Level> childLevels
}
I want to convert this class into the following:
public class ListLevel {
private List<ListLevel> childLevels
}
ChildLevels are not necessarily instantiated and may be null. I start with a Map<String, Level>
and I want to convert this into a List<ListLevel>
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<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());
}
Any suggestions?
edit: to clarify, I want to convert all nested maps to lists but maintain the same nested structure
答案1
得分: 2
你需要使用Map
的values()
方法,就像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<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());
}
After question Updated:
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());
}
Here ListLevel convertLevelToListLevel(Level level, List<ListLevel> childListLevels)
is converter for Level
to ListLevel
conversion as mention in comment OP already have.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论