From 971238704bd39ba1b1d90f4acc9723a0824b70fd Mon Sep 17 00:00:00 2001 From: Neetika Khandelwal Date: Sat, 3 Feb 2024 14:13:53 +0530 Subject: [PATCH] Stream file --- .../stream/range/StreamRangeUnitTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/range/StreamRangeUnitTest.java diff --git a/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/range/StreamRangeUnitTest.java b/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/range/StreamRangeUnitTest.java new file mode 100644 index 0000000000..cc29ff6fd6 --- /dev/null +++ b/core-java-modules/core-java-streams/src/test/java/com/baeldung/stream/range/StreamRangeUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.stream.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 testLimit() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + List expectedRange = Arrays.asList(3, 4, 5, 6, 7); + + List range = numbers.stream() + .skip(2) + .limit(5) + .collect(Collectors.toList()); + + assertEquals(expectedRange, range); + } + + @Test + public void testCollect() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + List expectedRange = Arrays.asList(3, 4, 5, 6, 7); + + List range = numbers.stream() + .filter(n -> n >= 3 && n <= 7) + .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); + + assertEquals(expectedRange, range); + } +}