将数据写入CSV文件使用Java 8流

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

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(&quot;src/test/resources/custData.csv&quot;);

    List&lt;String&gt; customers = new ArrayList&lt;&gt;(customers);
    List&lt;String&gt; references = new ArrayList&lt;&gt;(references);

    String collect1 = customers.stream().collect(Collectors.joining(&quot;,\n&quot; + &quot;:&quot;));
    String collect2 = references.stream().collect(Collectors.joining(&quot;,\n&quot; + &quot;:&quot;));

    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 -&gt; customers.get(i) + &quot;:&quot; + references.get(i))
                      .collect(Collectors.joining(&quot;,\n&quot;));

答案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 -&gt; customers.get(i) + &quot;:&quot; + references.get(i))
            .collect(Collectors.joining(&quot;\n&quot;));
writer.write(output);

答案3

得分: 1

我假设 customersreferences 的大小相同。您可以在 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) + &quot;:&quot; + references.get(i) + &quot;,\n&quot;

Try this:

String output = IntStream.range(0, customers.size()).boxed()
		.map(i -&gt; customers.get(i) + &quot;:&quot; + references.get(i) + &quot;,\n&quot;).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.

See full article here

In pure java you can do the solutions

IntStream.range(0, Math.min(customers.size(), references.size()))
         .mapToObj(i -&gt; customers.get(i) + &quot;:&quot; + references.get(i))
         .collect(Collectors.joining(&quot;,\n&quot;));

If you have guava you can do it bit nicer

Streams
    .zip(customers.stream(), references.stream(), (customer, reference) -&gt; customer + &quot;:&quot; + reference)
    .collect(Collectors.joining(&quot;,\n&quot;));

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

发表评论

匿名网友

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

确定