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

* 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?
This commit is contained in:
Yasin 2017-07-30 22:03:51 +05:30 committed by maibin
parent 6bfd4d3aae
commit deb79bbd83
3 changed files with 92 additions and 1 deletions

View File

@ -391,7 +391,7 @@
<logback.version>1.1.7</logback.version>
<!-- util -->
<guava.version>21.0</guava.version>
<guava.version>22.0</guava.version>
<commons-lang3.version>3.5</commons-lang3.version>
<bouncycastle.version>1.55</bouncycastle.version>
<commons-codec.version>1.10</commons-codec.version>

View File

@ -0,0 +1,69 @@
package com.baeldung.stream;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.junit.Test;
import com.baeldung.stream.mycollectors.MyImmutableListCollector;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
public class StreamToImmutableTest {
@Test
public void whenUsingCollectingToImmutableSet_thenSuccess() {
Set<String> mutableSet = new HashSet<>(Arrays.asList("a", "b", "c"));
mutableSet.add("test");
Set<String> immutableSet = mutableSet.stream()
.collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
System.out.println(immutableSet.getClass());
}
@Test
public void whenUsingCollectingToUnmodifiableList_thenSuccess() {
List<String> givenList = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> result = givenList.stream()
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
System.out.println(result.getClass());
}
@Test
public void whenCollectToImmutableList_thenSuccess() {
List<Integer> list = IntStream.range(0, 9)
.boxed()
.collect(ImmutableList.toImmutableList());
System.out.println(list.getClass());
}
@Test
public void whenCollectToMyImmutableListCollector_thenSuccess() {
List<String> givenList = Arrays.asList("a", "b", "c", "d");
List<String> result = givenList.stream()
.collect(MyImmutableListCollector.toImmutableList());
System.out.println(result.getClass());
}
@Test
public void whenPassingSupplier_thenSuccess() {
List<String> givenList = Arrays.asList("a", "b", "c", "d");
List<String> result = givenList.stream()
.collect(MyImmutableListCollector.toImmutableList(LinkedList::new));
System.out.println(result.getClass());
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.stream.mycollectors;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class MyImmutableListCollector {
public static <T, A extends List<T>> Collector<T, A, List<T>> toImmutableList(Supplier<A> supplier) {
return Collector.of(supplier, List::add, (left, right) -> {
left.addAll(right);
return left;
}, Collections::unmodifiableList);
}
public static <T> Collector<T, List<T>, List<T>> toImmutableList() {
return toImmutableList(ArrayList::new);
}
}