Java Stream How to - Convert List value to CSV data








Question

We would like to know how to convert List value to CSV data.

Answer

import java.util.ArrayList;
import java.util.List;
//  w ww  .j ava  2 s.c  om
public class Main {

    public static void main(String[] argv) {
        List<String> mascots = new ArrayList<>();
        mascots.add("duke");
        mascots.add("juggy");

        String actual = mascots.stream().
                reduce((t, u) -> t + "," + u).
                get();
        System.out.println(actual);
    }
}

The code above generates the following result.