forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
699cc10
commit 62089a7
Showing
67 changed files
with
880 additions
and
1,045 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
2 changes: 1 addition & 1 deletion
2
.../baeldung/breakforeach/CustomForEach.java → ...g/streams/breakforeach/CustomForEach.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ldung/breakforeach/CustomSpliterator.java → ...reams/breakforeach/CustomSpliterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...aeldung/breakforeach/CustomTakeWhile.java → ...streams/breakforeach/CustomTakeWhile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...eldung/breakforeach/TakeWhileExample.java → ...treams/breakforeach/TakeWhileExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...reach/BreakFromStreamForEachUnitTest.java → ...reach/BreakFromStreamForEachUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.breakforeach; | ||
package com.baeldung.streams.breakforeach; | ||
|
||
import org.junit.Test; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## Core Java streams | ||
|
||
This module contains articles about the Stream API in Java. | ||
|
||
### Relevant Articles: | ||
- [The Java 8 Stream API Tutorial](https://www.baeldung.com/java-8-streams) | ||
- [Introduction to Java 8 Streams](https://www.baeldung.com/java-8-streams-introduction) | ||
- [Java 8 Stream findFirst() vs. findAny()](https://www.baeldung.com/java-stream-findfirst-vs-findany) | ||
- [Guide to Stream.reduce()](https://www.baeldung.com/java-stream-reduce) | ||
- [Java IntStream Conversions](https://www.baeldung.com/java-intstream-convert) | ||
- [Java 8 Streams peek() API](https://www.baeldung.com/java-streams-peek-api) | ||
- [Working With Maps Using Streams](https://www.baeldung.com/java-maps-streams) | ||
- [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection) | ||
- [How to Add a Single Element to a Stream](https://www.baeldung.com/java-stream-append-prepend) | ||
- [Operating on and Removing an Item from Stream](https://www.baeldung.com/java-use-remove-item-stream) | ||
- More articles: [[<-- prev>]](/../core-java-streams) [[next -->]](/../core-java-streams-3) |
100 changes: 52 additions & 48 deletions
100
java-streams-2/pom.xml → ...-java-modules/core-java-streams-2/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
<groupId>com.baeldung.javastreams2</groupId> | ||
<artifactId>java-streams-2</artifactId> | ||
<version>1.0</version> | ||
<name>java-streams-2</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> | ||
<dependency> | ||
<groupId>org.openjdk.jmh</groupId> | ||
<artifactId>jmh-core</artifactId> | ||
<version>${jmh-core.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.openjdk.jmh</groupId> | ||
<artifactId>jmh-generator-annprocess</artifactId> | ||
<version>${jmh-generator.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
<type>jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.9</maven.compiler.source> | ||
<maven.compiler.target>1.9</maven.compiler.target> | ||
<assertj.version>3.11.1</assertj.version> | ||
</properties> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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-streams-2</artifactId> | ||
<version>1.0</version> | ||
<name>core-java-streams-2</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> | ||
<dependency> | ||
<groupId>org.openjdk.jmh</groupId> | ||
<artifactId>jmh-core</artifactId> | ||
<version>${jmh-core.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.openjdk.jmh</groupId> | ||
<artifactId>jmh-generator-annprocess</artifactId> | ||
<version>${jmh-generator.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>${log4j.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
<type>jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.9</maven.compiler.source> | ||
<maven.compiler.target>1.9</maven.compiler.target> | ||
<assertj.version>3.11.1</assertj.version> | ||
</properties> | ||
</project> |
78 changes: 39 additions & 39 deletions
78
...ldung/reduce/application/Application.java → ...ldung/reduce/application/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
package com.baeldung.reduce.application; | ||
|
||
import com.baeldung.reduce.entities.User; | ||
import com.baeldung.reduce.utilities.NumberUtils; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); | ||
int result1 = numbers.stream().reduce(0, (subtotal, element) -> subtotal + element); | ||
System.out.println(result1); | ||
|
||
int result2 = numbers.stream().reduce(0, Integer::sum); | ||
System.out.println(result2); | ||
|
||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e"); | ||
String result3 = letters.stream().reduce("", (partialString, element) -> partialString + element); | ||
System.out.println(result3); | ||
|
||
String result4 = letters.stream().reduce("", String::concat); | ||
System.out.println(result4); | ||
|
||
String result5 = letters.stream().reduce("", (partialString, element) -> partialString.toUpperCase() + element.toUpperCase()); | ||
System.out.println(result5); | ||
|
||
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35)); | ||
int result6 = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
System.out.println(result6); | ||
|
||
String result7 = letters.parallelStream().reduce("", String::concat); | ||
System.out.println(result7); | ||
|
||
int result8 = users.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
System.out.println(result8); | ||
} | ||
} | ||
package com.baeldung.reduce.application; | ||
|
||
import com.baeldung.reduce.entities.User; | ||
import com.baeldung.reduce.utilities.NumberUtils; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); | ||
int result1 = numbers.stream().reduce(0, (subtotal, element) -> subtotal + element); | ||
System.out.println(result1); | ||
|
||
int result2 = numbers.stream().reduce(0, Integer::sum); | ||
System.out.println(result2); | ||
|
||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e"); | ||
String result3 = letters.stream().reduce("", (partialString, element) -> partialString + element); | ||
System.out.println(result3); | ||
|
||
String result4 = letters.stream().reduce("", String::concat); | ||
System.out.println(result4); | ||
|
||
String result5 = letters.stream().reduce("", (partialString, element) -> partialString.toUpperCase() + element.toUpperCase()); | ||
System.out.println(result5); | ||
|
||
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35)); | ||
int result6 = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
System.out.println(result6); | ||
|
||
String result7 = letters.parallelStream().reduce("", String::concat); | ||
System.out.println(result7); | ||
|
||
int result8 = users.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
System.out.println(result8); | ||
} | ||
} |
104 changes: 52 additions & 52 deletions
104
.../benchmarks/JMHStreamReduceBenchMark.java → .../benchmarks/JMHStreamReduceBenchMark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
package com.baeldung.reduce.benchmarks; | ||
|
||
import com.baeldung.reduce.entities.User; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
@State(Scope.Thread) | ||
@BenchmarkMode(Mode.AverageTime) | ||
public class JMHStreamReduceBenchMark { | ||
|
||
private final List<User> userList = createUsers(); | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
|
||
Options options = new OptionsBuilder() | ||
.include(JMHStreamReduceBenchMark.class.getSimpleName()).threads(1) | ||
.forks(1).shouldFailOnError(true).shouldDoGC(true) | ||
.jvmArgs("-server").build(); | ||
new Runner(options).run(); | ||
} | ||
|
||
private List<User> createUsers() { | ||
List<User> users = new ArrayList<>(); | ||
for (int i = 0; i <= 1000000; i++) { | ||
users.add(new User("John" + i, i)); | ||
} | ||
return users; | ||
} | ||
|
||
@Benchmark | ||
public Integer executeReduceOnParallelizedStream() { | ||
return this.userList | ||
.parallelStream() | ||
.reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
} | ||
|
||
@Benchmark | ||
public Integer executeReduceOnSequentialStream() { | ||
return this.userList | ||
.stream() | ||
.reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
} | ||
} | ||
package com.baeldung.reduce.benchmarks; | ||
|
||
import com.baeldung.reduce.entities.User; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
@State(Scope.Thread) | ||
@BenchmarkMode(Mode.AverageTime) | ||
public class JMHStreamReduceBenchMark { | ||
|
||
private final List<User> userList = createUsers(); | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
|
||
Options options = new OptionsBuilder() | ||
.include(JMHStreamReduceBenchMark.class.getSimpleName()).threads(1) | ||
.forks(1).shouldFailOnError(true).shouldDoGC(true) | ||
.jvmArgs("-server").build(); | ||
new Runner(options).run(); | ||
} | ||
|
||
private List<User> createUsers() { | ||
List<User> users = new ArrayList<>(); | ||
for (int i = 0; i <= 1000000; i++) { | ||
users.add(new User("John" + i, i)); | ||
} | ||
return users; | ||
} | ||
|
||
@Benchmark | ||
public Integer executeReduceOnParallelizedStream() { | ||
return this.userList | ||
.parallelStream() | ||
.reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
} | ||
|
||
@Benchmark | ||
public Integer executeReduceOnSequentialStream() { | ||
return this.userList | ||
.stream() | ||
.reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); | ||
} | ||
} |
Oops, something went wrong.