英文:
Writing data to a .CSV file using a Java 8 Stream
问题
I will provide you with the translated code without any additional explanations or polite language:
我试图在Java中将2个数据集输出到一个.csv文件中。
数据集1 = 客户姓名
数据集2 = 客户参考
我希望.csv文件的呈现方式如下:
Smith:839393,
Johnson:283940,
Collins:293845
我的代码:
private void writeDataToFile() throws IOException {
FileWriter writer = new FileWriter("src/test/resources/custData.csv");
List<String> customers = new ArrayList<>(customers);
List<String> references = new ArrayList<>(references);
String collect1 = customers.stream().collect(Collectors.joining(",\n" + ":"));
String collect2 = references.stream().collect(Collectors.joining(",\n" + ":"));
writer.write(collect1 + collect2);
writer.close();
}
我的输出:
Smith,
:Johnson,
:Collins839393,
:283940,
:293845
如何实现所需的输出?
英文:
I am trying to output 2 collections of data to a .csv file in Java.
Collection 1 = customer names
Collection 2 = customer references
I want the .csv to present as:
Smith:839393,
Johnson:283940,
Collins:293845
My code:
private void writeDataToFile() throws IOException {
FileWriter writer = new FileWriter("src/test/resources/custData.csv");
List<String> customers = new ArrayList<>(customers);
List<String> references = new ArrayList<>(references);
String collect1 = customers.stream().collect(Collectors.joining(",\n" + ":"));
String collect2 = references.stream().collect(Collectors.joining(",\n" + ":"));
writer.write(collect1 + collect2);
writer.close();
}
My output:
Smith,
:Johnson,
:Collins839393,
:283940,
:293845
How can I achieve the desired output?
答案1
得分: 2
如果两个列表大小相同,可以按以下方式操作。使用IntStream.range
来遍历列表,然后映射数据。然后使用Collectors.joining
来收集,使用,\n
进行连接:
String res = IntStream.range(0, customers.size())
.mapToObj(i -> customers.get(i) + ":" + references.get(i))
.collect(Collectors.joining(",\n"));
英文:
You can do this way if both lists have the same size. Use IntStream.range
to iterate the lists and then map the data. Then collect joining ,\n
String res = IntStream.range(0, customers.size())
.mapToObj(i -> customers.get(i) + ":" + references.get(i))
.collect(Collectors.joining(",\n"));
答案2
得分: 2
假设你的两个集合元素数量相同,你可以尝试这样做:
String output =
IntStream.rangeClosed(0, customers.size()-1)
.boxed()
.map(i -> customers.get(i) + ":" + references.get(i))
.collect(Collectors.joining("\n"));
writer.write(output);
英文:
Assuming both of your collections have same number of elements you can try this
String output =
IntStream.rangeClosed(0, customers.size()-1)
.boxed()
.map(i -> customers.get(i) + ":" + references.get(i))
.collect(Collectors.joining("\n"));
writer.write(output);
答案3
得分: 1
我假设 customers
和 references
的大小相同。您可以在 0 和 customers.size()
之间进行迭代,并组合两个列表的元素:
customers.get(i) + ":" + references.get(i) + ",\n";
尝试这样做:
String output = IntStream.range(0, customers.size()).boxed()
.map(i -> customers.get(i) + ":" + references.get(i) + ",\n").collect(Collectors.joining());
英文:
I assume customers
and references
have the same size. You can iterate between 0 and customers.size()
and combine the elements of both lists:
customers.get(i) + ":" + references.get(i) + ",\n"
Try this:
String output = IntStream.range(0, customers.size()).boxed()
.map(i -> customers.get(i) + ":" + references.get(i) + ",\n").collect(Collectors.joining());
答案4
得分: 0
你正在尝试做的事情被称为集合合并。
在纯Java中,你可以使用以下解决方案:
IntStream.range(0, Math.min(customers.size(), references.size()))
.mapToObj(i -> customers.get(i) + ":" + references.get(i))
.collect(Collectors.joining(",\n"));
如果你使用Guava库,你可以稍微更简洁地实现:
Streams
.zip(customers.stream(), references.stream(), (customer, reference) -> customer + ":" + reference)
.collect(Collectors.joining(",\n"));
英文:
What you are trying to do is called collection zipping.
In pure java you can do the solutions
IntStream.range(0, Math.min(customers.size(), references.size()))
.mapToObj(i -> customers.get(i) + ":" + references.get(i))
.collect(Collectors.joining(",\n"));
If you have guava you can do it bit nicer
Streams
.zip(customers.stream(), references.stream(), (customer, reference) -> customer + ":" + reference)
.collect(Collectors.joining(",\n"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论