gpt4 book ai didi

java - 使用流通过 Builder 从 csv 文件创建对象

转载 作者:行者123 更新时间:2023-12-02 11:21:19 26 4
gpt4 key购买 nike

我想使用 PlanetBuilder 和流从 .csv 文件创建一个“Planet”对象。到目前为止,我只能读取 csv 文件并为每一行创建一个 Planet 对象,但不幸的是我不知道如何将构造函数值分配给该对象,所以它基本上只是创建一个空的 Planet 对象每行。有人可以告诉我如何为每行分配值吗?

创建对象后,我会将它们添加到行星列表

这是创建新行星对象的方法。

public List<Planet> csvDataToPlanets(String filePath) {
List<Planet> planets = new ArrayList<>();
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
List<List<String>> places = lines.skip(1).map(line -> Arrays.asList(line.split(","))).collect(Collectors.toList());
places.forEach(place -> new PlanetBuilder().createPlanet());
} catch (IOException e) {
e.printStackTrace();
}
return planets;
}

这是 CSV 文件。

planet_name,inhabitants,has_stargate,has_dhd,teams_visited
Abydos,0,True,False,[sg-1]
Adara II,117778380,True,True,[sg-1]
Adora,0,False,False,[sg-1]
Alaris,13663,True,True,[sg-1]
Alebran's planet,186402846,False,False,[sg-1; sg-6]
Alpha Site,460849025,True,True,[sg-1; sg-4]
Altair,8087,False,False,[sg-1; sg-2]
Amon Shek,0,True,True,[sg-1; sg-9]
Amra,467925616,True,False,[sg-1]
Apophis's training camp,10188,True,True,[sg-1]
Ardena,13693,True,True,[sg-1; sg-5]
Argos,0,True,True,[sg-1]
Arkad's planet,257318689,True,False,[sg-1; sg-14]
Arkhan's planet,464136466,False,False,[sg-1]

以及我的 Planet 类和 Planetbuilder 类

public class Planet {
private String name;
private long inhabitants;
private boolean stargateAvailable;
private boolean dhdAvailable;
private List<String> teamsVisited;


public Planet(String name, long inhabitants, boolean stargateAvailable, boolean dhdAvailable, List<String> teamsVisited) {
}

public String getName() {
return name;
}

public long getInhabitants() {
return inhabitants;
}

public boolean isStargateAvailable() {
return stargateAvailable;
}

public boolean isDhdAvailable() {
return dhdAvailable;
}

public List<String> getTeamsVisited() {
return teamsVisited;
}

@Override
public String toString() {
return name;
}


public class PlanetBuilder {
private String name;
private long inhabitants;
private boolean stargateAvailable;
private boolean dhdAvailable;
private List<String> teamsVisited;

public PlanetBuilder Name(String name) {
this.name = name;
return this;
}

public PlanetBuilder Inhabitants(long inhabitants) {
this.inhabitants = inhabitants;
return this;
}

public PlanetBuilder StargateAvailable(boolean stargateAvailable) {
this.stargateAvailable = stargateAvailable;
return this;
}

public PlanetBuilder DhdAvailable(boolean dhdAvailable) {
this.dhdAvailable = dhdAvailable;
return this;
}

public PlanetBuilder TeamsVisited(List<String> teamsVisited) {
this.teamsVisited = teamsVisited;
return this;
}

public Planet createPlanet() {
return new Planet(name, inhabitants, stargateAvailable, dhdAvailable, teamsVisited);
}

最佳答案

最简单的答案是扩展 places.forEach(place ->...) 内部的逻辑调用,以便您从 place 中获取每个值(这是值的 List<String>)并为每个属性添加对构建器的调用(根据需要解析它们)。例如。 places.forEach(place -> new PlanetBuilder().Name(place.get(0)).createPlanet()); (在这种情况下不需要解析)

但这绝对不是最好的解决方案。

理想情况下,您会转向使用像 http://beanio.org/ 这样的库或http://flatworm.sourceforge.net/为您处理解码工作。

关于java - 使用流通过 Builder 从 csv 文件创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49898517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com