Split or move java-streams module
This commit is contained in:
6
core-java-modules/core-java-9-streams/README.md
Normal file
6
core-java-modules/core-java-9-streams/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
## Core Java 9 streams
|
||||
|
||||
This module contains articles about Java 9 streams
|
||||
|
||||
### Relevant Articles:
|
||||
- [How to Break from Java Stream forEach](https://www.baeldung.com/java-break-stream-foreach)
|
||||
31
core-java-modules/core-java-9-streams/pom.xml
Normal file
31
core-java-modules/core-java-9-streams/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-9-streams</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-9-streams</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-9-streams</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.streams.breakforeach;
|
||||
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CustomForEach {
|
||||
|
||||
public static class Breaker {
|
||||
private boolean shouldBreak = false;
|
||||
|
||||
public void stop() {
|
||||
shouldBreak = true;
|
||||
}
|
||||
|
||||
boolean get() {
|
||||
return shouldBreak;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> void forEach(Stream<T> stream, BiConsumer<T, Breaker> consumer) {
|
||||
Spliterator<T> spliterator = stream.spliterator();
|
||||
boolean hadNext = true;
|
||||
Breaker breaker = new Breaker();
|
||||
|
||||
while (hadNext && !breaker.get()) {
|
||||
hadNext = spliterator.tryAdvance(elem -> {
|
||||
consumer.accept(elem, breaker);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.streams.breakforeach;
|
||||
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class CustomSpliterator<T> extends Spliterators.AbstractSpliterator<T> {
|
||||
|
||||
private Spliterator<T> splitr;
|
||||
private Predicate<T> predicate;
|
||||
private boolean isMatched = true;
|
||||
|
||||
public CustomSpliterator(Spliterator<T> splitr, Predicate<T> predicate) {
|
||||
super(splitr.estimateSize(), 0);
|
||||
this.splitr = splitr;
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super T> consumer) {
|
||||
boolean hadNext = splitr.tryAdvance(elem -> {
|
||||
if (predicate.test(elem) && isMatched) {
|
||||
consumer.accept(elem);
|
||||
} else {
|
||||
isMatched = false;
|
||||
}
|
||||
});
|
||||
return hadNext && isMatched;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.streams.breakforeach;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
public class CustomTakeWhile {
|
||||
|
||||
public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<T> predicate) {
|
||||
CustomSpliterator<T> customSpliterator = new CustomSpliterator<>(stream.spliterator(), predicate);
|
||||
return StreamSupport.stream(customSpliterator, false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.streams.breakforeach;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class TakeWhileExample {
|
||||
|
||||
public static void takeWhileJava9() {
|
||||
Stream.of("cat", "dog", "elephant", "fox", "rabbit", "duck")
|
||||
.takeWhile(n -> n.length() % 2 != 0)
|
||||
.forEach(System.out::println); // cat, dog
|
||||
}
|
||||
|
||||
public static void plainForLoopWithBreak() {
|
||||
List<String> list = asList("cat", "dog", "elephant", "fox", "rabbit", "duck");
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
String item = list.get(i);
|
||||
if (item.length() % 2 == 0) {
|
||||
break;
|
||||
}
|
||||
System.out.println(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.streams.breakforeach;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BreakFromStreamForEachUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCustomTakeWhileIsCalled_ThenCorrectItemsAreReturned() {
|
||||
Stream<String> initialStream = Stream.of("cat", "dog", "elephant", "fox", "rabbit", "duck");
|
||||
|
||||
List<String> result = CustomTakeWhile.takeWhile(initialStream, x -> x.length() % 2 != 0)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(asList("cat", "dog"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCustomForEachIsCalled_ThenCorrectItemsAreReturned() {
|
||||
Stream<String> initialStream = Stream.of("cat", "dog", "elephant", "fox", "rabbit", "duck");
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
CustomForEach.forEach(initialStream, (elem, breaker) -> {
|
||||
if (elem.length() % 2 == 0) {
|
||||
breaker.stop();
|
||||
} else {
|
||||
result.add(elem);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(asList("cat", "dog"), result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user