Search in sources :

Example 1 with Collector

use of java8.util.stream.Collector in project streamsupport by stefan-zobel.

the class CollectorsTest method testJoining.

@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testJoining(String name, TestData.OfRef<Integer> data) throws Exception {
    withData(data).terminal(s -> s.map(Object::toString).collect(Collectors.joining())).expectedResult(join(data, "")).exercise();
    Collector<String, StringBuilder, String> likeJoining = Collectors.of(StringBuilder::new, StringBuilder::append, (sb1, sb2) -> sb1.append(sb2.toString()), StringBuilder::toString);
    withData(data).terminal(s -> s.map(Object::toString).collect(likeJoining)).expectedResult(join(data, "")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(Collectors.joining(","))).expectedResult(join(data, ",")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(Collectors.joining(",", "[", "]"))).expectedResult("[" + join(data, ",") + "]").exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString()).expectedResult(join(data, "")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(() -> new StringJoiner(","), (sj, cs) -> sj.add(cs), (j1, j2) -> j1.merge(j2)).toString()).expectedResult(join(data, ",")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(() -> new StringJoiner(",", "[", "]"), (sj, cs) -> sj.add(cs), (j1, j2) -> j1.merge(j2)).toString()).expectedResult("[" + join(data, ",") + "]").exercise();
}
Also used : StringJoiner(java8.util.StringJoiner) Test(org.testng.annotations.Test)

Aggregations

StringJoiner (java8.util.StringJoiner)1 Test (org.testng.annotations.Test)1