BAEL-6715: Taking Every Nth Element from a Java Stream (#14974)
* BAEL-6715: Taking Every Nth Element from a Java Stream * BAEL-6715: Renaming the tests
This commit is contained in:
parent
6a8a8ba9c2
commit
268698089d
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.skippingelements;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.stream.Collector;
|
||||||
|
|
||||||
|
class SkippingCollector {
|
||||||
|
private static final BinaryOperator<SkippingCollector> IGNORE_COMBINE = (a, b) -> a;
|
||||||
|
private final int skip;
|
||||||
|
private final List<String> list = new ArrayList<>();
|
||||||
|
private int currentIndex = 0;
|
||||||
|
private SkippingCollector(int skip) {
|
||||||
|
this.skip = skip;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void accept(String item) {
|
||||||
|
final int index = ++currentIndex % skip;
|
||||||
|
if (index == 0)
|
||||||
|
list.add(item);
|
||||||
|
}
|
||||||
|
private List<String> getResult() {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Collector<String, SkippingCollector, List<String>> collector(int skip) {
|
||||||
|
return Collector.of(() -> new SkippingCollector(skip),
|
||||||
|
SkippingCollector::accept,
|
||||||
|
IGNORE_COMBINE,
|
||||||
|
SkippingCollector::getResult);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.baeldung.skippingelements;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class SkippingElements {
|
||||||
|
|
||||||
|
private SkippingElements() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> skipNthElementInListWithFilter(List<String> sourceList, int n) {
|
||||||
|
return IntStream.range(0, sourceList.size())
|
||||||
|
.filter(s -> (s + 1) % n == 0)
|
||||||
|
.mapToObj(sourceList::get)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> skipNthElementInListWithIterate(List<String> sourceList, int n) {
|
||||||
|
int limit = sourceList.size() / n;
|
||||||
|
return IntStream.iterate(n - 1, i -> (i + n))
|
||||||
|
.limit(limit)
|
||||||
|
.mapToObj(sourceList::get)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> skipNthElementInListWithSublist(List<String> sourceList, int n) {
|
||||||
|
int limit = sourceList.size() / n;
|
||||||
|
return Stream.iterate(sourceList, s -> s.subList(n, s.size()))
|
||||||
|
.limit(limit)
|
||||||
|
.map(s -> s.get(n - 1))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> skipNthElementInListWithFor(List<String> sourceList, int n) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
for (int i = n - 1; i < sourceList.size(); i += n) {
|
||||||
|
result.add(sourceList.get(i));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> skipNthElementInListWithIterator(Stream<String> sourceStream, int n) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
final Iterator<String> iterator = sourceStream.iterator();
|
||||||
|
int count = 0;
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (count % n == n - 1) {
|
||||||
|
result.add(iterator.next());
|
||||||
|
} else {
|
||||||
|
iterator.next();
|
||||||
|
}
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> skipNthElementInStreamWithCollector(Stream<String> sourceStream, int n) {
|
||||||
|
return sourceStream.collect(SkippingCollector.collector(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.baeldung.skippingelements;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
class SkippingElementsUnitTest {
|
||||||
|
|
||||||
|
private static Stream<Arguments> testSource() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
|
||||||
|
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
|
||||||
|
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
|
||||||
|
"Thirty One", "Thirty Two", "Thirty Three"),
|
||||||
|
List.of("Three", "Six", "Nine", "Twelve", "Fifteen", "Eighteen", "Twenty One", "Twenty Four", "Twenty Seven", "Thirty", "Thirty Three"),
|
||||||
|
3),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
|
||||||
|
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
|
||||||
|
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
|
||||||
|
"Thirty One", "Thirty Two", "Thirty Three"),
|
||||||
|
List.of("Five", "Ten", "Fifteen", "Twenty", "Twenty Five", "Thirty"),
|
||||||
|
5),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
|
||||||
|
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
|
||||||
|
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
|
||||||
|
"Thirty One", "Thirty Two", "Thirty Three"),
|
||||||
|
List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
|
||||||
|
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two",
|
||||||
|
"Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
|
||||||
|
"Thirty One", "Thirty Two", "Thirty Three"),
|
||||||
|
1),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
|
||||||
|
List.of("Wednesday", "Saturday"),
|
||||||
|
3),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
|
||||||
|
List.of("Friday"),
|
||||||
|
5),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
|
||||||
|
List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
|
||||||
|
1),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
|
||||||
|
"December"),
|
||||||
|
List.of("March", "June", "September", "December"),
|
||||||
|
3),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
|
||||||
|
"December"),
|
||||||
|
List.of("May", "October"),
|
||||||
|
5),
|
||||||
|
Arguments.of(
|
||||||
|
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
|
||||||
|
"December"),
|
||||||
|
List.of("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
|
||||||
|
"December"),
|
||||||
|
1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testSource")
|
||||||
|
void givenListSkipNthElementInListWithFilterTestShouldFilterNthElement(List<String> input, List<String> expected, int n) {
|
||||||
|
final List<String> actual = SkippingElements.skipNthElementInListWithFilter(input, n);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testSource")
|
||||||
|
void givenListSkipNthElementInListWithIterateTestShouldFilterNthElement(List<String> input, List<String> expected, int n) {
|
||||||
|
final List<String> actual = SkippingElements.skipNthElementInListWithIterate(input, n);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testSource")
|
||||||
|
void givenListSkipNthElementInListWithSublistTestShouldFilterNthElement(List<String> input, List<String> expected, int n) {
|
||||||
|
final List<String> actual = SkippingElements.skipNthElementInListWithSublist(input, n);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testSource")
|
||||||
|
void givenListSkipNthElementInListWithForTestShouldFilterNthElement(List<String> input, List<String> expected, int n) {
|
||||||
|
final List<String> actual = SkippingElements.skipNthElementInListWithFor(input, n);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testSource")
|
||||||
|
void givenListSkipNthElementInStreamWithIteratorTestShouldFilterNthElement(List<String> input, List<String> expected, int n) {
|
||||||
|
final Stream<String> inputStream = input.stream();
|
||||||
|
final List<String> actual = SkippingElements.skipNthElementInListWithIterator(inputStream, n);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testSource")
|
||||||
|
void givenListSkipNthElementInStreamWithCollectorShouldFilterNthElement(List<String> input, List<String> expected, int n) {
|
||||||
|
final Stream<String> inputStream = input.stream();
|
||||||
|
final List<String> actual = SkippingElements.skipNthElementInStreamWithCollector(inputStream, n);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user