2017-07-30 22:03:51 +05:30
|
|
|
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.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import com.baeldung.stream.mycollectors.MyImmutableListCollector;
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
|
2018-05-30 01:44:19 +05:30
|
|
|
public class StreamToImmutableUnitTest {
|
2017-07-30 22:03:51 +05:30
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenUsingCollectingToImmutableSet_thenSuccess() {
|
2017-08-02 20:57:25 +05:30
|
|
|
List<String> givenList = Arrays.asList("a", "b", "c");
|
|
|
|
|
List<String> result = givenList.stream()
|
|
|
|
|
.collect(collectingAndThen(toSet(), ImmutableList::copyOf));
|
2017-07-30 22:03:51 +05:30
|
|
|
|
2017-08-02 20:57:25 +05:30
|
|
|
System.out.println(result.getClass());
|
2017-07-30 22:03:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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());
|
|
|
|
|
}
|
|
|
|
|
}
|