BAEL-887 How to collect a Java Stream to an immutable collection? (#2356)

* BAEL-900 Guide to dynamic tests in Junit 5

* BAEL-900 Guide to Dynamic Tests in Junit 5

* Revert "BAEL-900 Guide to Dynamic Tests in Junit 5"

This reverts commit d0d45c9067223347da20d0f2c80de391fcade38e.

* BAEL-900 Guide to Dynamic Tests in Junit 5

* BAEL-900 Guide to dynamic tests in Junit 5

* removed unnecessary annotation

* BAEL-900 unused imports removed

* BAEL-900 simplified input generator code

* BAEL-252 A Java Client to consume a WebSockets API

* BAEL-887 How to collect a Java Stream to an immutable collection?

* BAEL-887 How to collect a Java Stream to an immutable collection?
This commit is contained in:
Yasin 2017-08-02 20:57:25 +05:30 committed by maibin
parent b6b077b457
commit f02d117223
1 changed files with 4 additions and 8 deletions

View File

@ -7,28 +7,24 @@ import static java.util.stream.Collectors.toSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.junit.Test; import org.junit.Test;
import com.baeldung.stream.mycollectors.MyImmutableListCollector; import com.baeldung.stream.mycollectors.MyImmutableListCollector;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
public class StreamToImmutableTest { public class StreamToImmutableTest {
@Test @Test
public void whenUsingCollectingToImmutableSet_thenSuccess() { public void whenUsingCollectingToImmutableSet_thenSuccess() {
Set<String> mutableSet = new HashSet<>(Arrays.asList("a", "b", "c")); List<String> givenList = Arrays.asList("a", "b", "c");
mutableSet.add("test"); List<String> result = givenList.stream()
Set<String> immutableSet = mutableSet.stream() .collect(collectingAndThen(toSet(), ImmutableList::copyOf));
.collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
System.out.println(immutableSet.getClass()); System.out.println(result.getClass());
} }
@Test @Test