JAVA-23082 Review log statements for projects - Week 16 - 2023 (moved-13) (conti-1)

This commit is contained in:
timis1
2023-07-10 11:22:34 +03:00
committed by n
parent c998a6ffd6
commit 9b39c0ffaa
14 changed files with 187 additions and 15 deletions

View File

@@ -1,57 +1,59 @@
package com.baeldung.streams;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import java.util.*;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StreamToImmutableUnitTest {
import org.junit.jupiter.api.Test;
class StreamToImmutableUnitTest {
@Test
public void whenUsingCollectingToImmutableSet_thenSuccess() {
void whenUsingCollectingToImmutableSet_thenSuccess() {
List<String> givenList = Arrays.asList("a", "b", "c");
List<String> result = givenList.stream()
.collect(collectingAndThen(toSet(), ImmutableList::copyOf));
System.out.println(result.getClass());
assertEquals("com.google.common.collect.RegularImmutableList", result.getClass().getName());
}
@Test
public void whenUsingCollectingToUnmodifiableList_thenSuccess() {
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());
assertEquals("java.util.Collections$UnmodifiableRandomAccessList", result.getClass().getName());
}
@Test
public void whenCollectToImmutableList_thenSuccess() {
void whenCollectToImmutableList_thenSuccess() {
List<Integer> list = IntStream.range(0, 9)
.boxed()
.collect(ImmutableList.toImmutableList());
System.out.println(list.getClass());
assertEquals("com.google.common.collect.RegularImmutableList", list.getClass().getName());
}
@Test
public void whenCollectToMyImmutableListCollector_thenSuccess() {
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());
assertEquals("java.util.Collections$UnmodifiableRandomAccessList", result.getClass().getName());
}
@Test
public void whenPassingSupplier_thenSuccess() {
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());
assertEquals("java.util.Collections$UnmodifiableList", result.getClass().getName());
}
}