Java 如何创建列表<;对象>;带字段字符串和映射<;字符串,Set<;字符串>&燃气轮机;从另一个列表中<;对象2>;
类Java 如何创建列表<;对象>;带字段字符串和映射<;字符串,Set<;字符串>&燃气轮机;从另一个列表中<;对象2>;,java,list,for-loop,set,java-stream,Java,List,For Loop,Set,Java Stream,类Object2具有标准的getter,并且具有String字段文件夹,文件,以及版本。 它被命名为SourceInfo List source包含上述三个字段 我的目标是从列表创建一个列表 新的列表的类别为Info,如下所示 public class Info { private final String folder; private final Map<String, Set<String>> file; pu
Object2
具有标准的getter,并且具有String
字段文件夹
,文件
,以及版本
。
它被命名为SourceInfo
List source
包含上述三个字段
我的目标是从列表
创建一个列表
新的列表
的类别为Info
,如下所示
public class Info {
private final String folder;
private final Map<String, Set<String>> file;
public static Builder builder() {
return new Builder();
}
public static Builder builder(Info info) {
return new Builder(info);
}
private Info(Builder builder) {
this.folder = builder.folder;
this.file = builder.file;
}
public String getFolder() {
return folder;
}
public Map<String, Set<String>> getFile() {
return file;
}
// autogenerated toString, hashCode, and equals
public static class Builder {
private String folder;
private Map<String, Set<String>> file;
private Builder() {}
private Builder(Info info) {
this.folder = info.folder;
this.file = info.file;
}
public Builder with(Consumer<Builder> consumer) {
consumer.accept(this);
return this;
}
public Builder withFolder(String folder) {
this.folder = folder;
return this;
}
public Builder withFile(Map<String, Set<String>> file) {
this.file = file;
return this;
}
public Info build() {
return new Info(this);
}
}
公共类信息{
私有最终字符串文件夹;
私有最终地图文件;
公共静态生成器(){
返回新的生成器();
}
公共静态生成器(信息){
返回新的生成器(信息);
}
私人信息(建筑商){
this.folder=builder.folder;
this.file=builder.file;
}
公共字符串getFolder(){
返回文件夹;
}
公共映射getFile(){
返回文件;
}
//自动生成的toString、hashCode和equals
公共静态类生成器{
私有字符串文件夹;
私有地图文件;
私有生成器(){}
私人建筑商(信息){
this.folder=info.folder;
this.file=info.file;
}
公共建筑商(消费者){
消费者。接受(这个);
归还这个;
}
公用生成器withFolder(字符串文件夹){
this.folder=文件夹;
归还这个;
}
公共生成器withFile(地图文件){
this.file=文件;
归还这个;
}
公共信息构建(){
返回新信息(此);
}
}
到目前为止,我尝试的是在生成器模式中创建一个集合
List<SourceInfo> source;
// error: gc overhead limit exceeded
List<Info> infoList = source.stream()
.map(e -> Info.builder()
.withFolder(e.getFolder())
.withFile(source.stream()
.collect(Collectors.groupingBy(SourceInfo::getKey,
Collectors.mapping(SourceInfo::getVersion, Collectors.toSet()))))
.build())
.collect(Collectors.toList());
Map<String, Set<String>> map = source.stream()
.collect(Collectors
.groupingBy(SourceInfo::getKey,
Collectors.mapping(SourceInfo::getVersion, Collectors.toSet())));
List<Info> info = source.stream()
.map(e -> Info.builder()
.withFolder(e.getFolder())
.withFile(map.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue)))
.build())
.collect(Collectors.toList());
列出来源;
//错误:超出gc开销限制
List infoList=source.stream()
.map(e->Info.builder()
.withFolder(例如getFolder())
.withFile(source.stream()
.collect(收集器.groupingBy(SourceInfo::getKey、,
Collectors.mapping(SourceInfo::getVersion,Collectors.toSet()))
.build())
.collect(Collectors.toList());
Map=source.stream()
.收藏(收藏家)
.groupingBy(SourceInfo::getKey,
Collectors.mapping(SourceInfo::getVersion,Collectors.toSet());
List info=source.stream()
.map(e->Info.builder()
.withFolder(例如getFolder())
.withFile(map.entrySet())
.stream()
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue)))
.build())
.collect(Collectors.toList());
所需输出。以下语法可能已关闭
// [String, Map<String, Set<String>>]
Info [folder, [key=file [value=version]]]
...
/[字符串,映射]
信息[文件夹,[key=file[value=version]]
...
我是Java新手,非常感谢您的帮助
我想了解如何使用java8和for循环来实现这一点
谢谢。首先收集到
Map.entrySet()中
.stream()
.map(条目->新建信息生成器()
.withFolder(entry.getKey())
.withFile(entry.getValue())
.build()
)
.collect(收集器.toList())
));
这不会创建带有字符串文件夹或映射文件的信息对象,而收集器的第三个#收集器
会强制创建一个新的构造函数,该构造函数为文件夹,文件返回空值。正在寻找一种方法来构建信息没有新字段。信息不能编译。很遗憾,你能先修复它吗?然后我会检查。是的,它现在应该可以编译了。Thanks@zzzhw3137我修改:)你能检查一下它是否正常吗?它有效。谢谢!