Stream Range test class

This commit is contained in:
Neetika Khandelwal 2024-02-10 14:41:30 +05:30
parent cbfce5d0fc
commit e96a590105
1 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.baeldung.streams.range;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StreamRangeUnitTest {
@Test
public void whenRangeStreamUsingLimitSkip_thenPrintsRange() {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> expectedRange = Arrays.asList(3, 4, 5, 6, 7);
List<Integer> range = numbers.stream()
.skip(2)
.limit(5)
.collect(Collectors.toList());
assertEquals(expectedRange, range);
}
@Test
public void whenRangeStreamUsingCollectingAndThen_thenPrintsRange() {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> expectedRange = Arrays.asList(3, 4, 5, 6, 7);
List<Integer> range = numbers.stream()
.filter(n -> n >= 3 && n <= 7)
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
assertEquals(expectedRange, range);
}
}