adam bien's blog

Java 8: Reducing a List Into A CSV String 📎


import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import org.junit.Test;

public class CSVStreamTest {

    @Test
    public void listToString() {
        List<String> mascots = new ArrayList<>();
        mascots.add("duke");
        mascots.add("juggy");

        String expected = "duke,juggy";
        String actual = mascots.stream().
                reduce((t, u) -> t + "," + u).
                get();
        assertThat(actual, is(expected));
    }
}

See you at Java EE Workshops at MUC Airport!