Skip to content

Commit

Permalink
Split or move java-streams module
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-burcea committed Oct 27, 2019
1 parent 699cc10 commit 62089a7
Show file tree
Hide file tree
Showing 67 changed files with 880 additions and 1,045 deletions.
6 changes: 6 additions & 0 deletions core-java-modules/core-java-9-streams/README.md
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)
31 changes: 31 additions & 0 deletions core-java-modules/core-java-9-streams/pom.xml
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>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.breakforeach;
package com.baeldung.streams.breakforeach;

import java.util.Spliterator;
import java.util.function.BiConsumer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.breakforeach;
package com.baeldung.streams.breakforeach;

import java.util.Spliterator;
import java.util.Spliterators;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.breakforeach;
package com.baeldung.streams.breakforeach;

import java.util.function.Predicate;
import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.breakforeach;
package com.baeldung.streams.breakforeach;

import java.util.List;
import java.util.stream.Stream;
Expand Down
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;

Expand Down
16 changes: 16 additions & 0 deletions core-java-modules/core-java-streams-2/README.md
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)
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>
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);
}
}
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);
}
}
Loading

0 comments on commit 62089a7

Please sign in to comment.