Skip to content

Commit

Permalink
BAEL-887 How to collect a Java Stream to an immutable collection? (eu…
Browse files Browse the repository at this point in the history
…genp#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 d0d45c9.

* 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?
  • Loading branch information
yasin3061 authored and maibin committed Aug 2, 2017
1 parent b6b077b commit f02d117
Showing 1 changed file with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@
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));
List<String> givenList = Arrays.asList("a", "b", "c");
List<String> result = givenList.stream()
.collect(collectingAndThen(toSet(), ImmutableList::copyOf));

System.out.println(immutableSet.getClass());
System.out.println(result.getClass());
}

@Test
Expand Down

0 comments on commit f02d117

Please sign in to comment.