Skip to content

Commit

Permalink
finalizing streams updates sip
Browse files Browse the repository at this point in the history
  • Loading branch information
wkorando committed Aug 26, 2021
1 parent c61cfd1 commit f763b60
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 81 deletions.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions 017-streams-updates/iterator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# iterate with Predicate

```java
```

## Output
2 changes: 2 additions & 0 deletions xxx-streams-updates/not.md → 017-streams-updates/not.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# not

```java
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ofNullable

```java
```

Expand Down
5 changes: 2 additions & 3 deletions xxx-streams-updates/pom.xml → 017-streams-updates/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>1.0.0</version>
</parent>

<artifactId>xxx-soj-streams-updates</artifactId>
<name>xxx-soj-streams-updates</name>
<artifactId>017-soj-streams-updates</artifactId>
<name>017-soj-streams-updates</name>
<packaging>jar</packaging>

<description>Demonstrating Unix Sockets</description>
Expand All @@ -22,5 +22,4 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

</project>
39 changes: 39 additions & 0 deletions 017-streams-updates/script.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Hey Java developers,

Enjoying Streams since Java 8,

but finding some holes in its functionality?

[ ]

Streams and its supporting types

have seen several updates in recent releases

let's take a look at a few key changes

Teeing added to Collectors in Java 12

Allows the results of two downstream collectors to be merged

Predicate was updated in Java 11 with the static not

allowing for easier definition of a negation predicate

ofNullable added in Java 9,

provides null safety when processing a single item stream,

returning the value if non-null,

or an empty stream if the passed in value is null

iterate was also updated with java 9,

allowing for a predicate to be provided

which functions as a hasNext

and is checked before executing the "next" UnaryOperator

Happy coding!
17 changes: 17 additions & 0 deletions 017-streams-updates/src/main/java/PredicateNotJava11.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.function.Predicate;
import java.util.stream.Stream;

public class PredicateNotJava11 {
record Employee(String name, int monthsEmployed, boolean executive) {}

public static void main(String[] args) {
Stream<Employee> streamOfEmployees =
Stream.of(new Employee("Homer", 187, false),
new Employee("Lenny", 122, false),
new Employee("Carl", 93, false),
new Employee("Montgomery", 552, true));

streamOfEmployees.filter(Predicate.not(Employee::executive)).forEach(System.out::println);
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import java.util.function.Predicate;
import java.util.stream.Stream;

public class PredicateNot {
public class PredicateNotJava8 {
record Employee(String name, int monthsEmployed, boolean executive) {}

public static void main(String[] args) {
Stream<Employee> streamOfEmployees = Stream.of(new Employee("Homer", 187, false),
new Employee("Lenny", 122, false), new Employee("Carl", 93, false),
Stream<Employee> streamOfEmployees =
Stream.of(new Employee("Homer", 187, false),
new Employee("Lenny", 122, false),
new Employee("Carl", 93, false),
new Employee("Montgomery", 552, true));
Predicate<Employee> isExecutive = Employee::executive;
streamOfEmployees.filter(Predicate.not(isExecutive)).forEach(System.out::println);

streamOfEmployees.filter(isExecutive.negate()).forEach(System.out::println);
}

}
22 changes: 22 additions & 0 deletions 017-streams-updates/src/main/java/StreamOfNullable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

public class StreamOfNullable {
record Employee(String name, int monthsEmployed, boolean executive) {
}

public static void main(String[] args) {
List<Employee> streamOfEmployees = null;
System.out.println("streamOfEmployees is null");
Stream.ofNullable(streamOfEmployees).flatMap(Collection::stream).forEach(System.out::println);

streamOfEmployees = List.of(new Employee("Homer", 187, false),
new Employee("Lenny", 122, false),
new Employee("Carl", 93, false),
new Employee("Montgomery", 552, true));

System.out.println("streamOfEmployees is not null");
Stream.ofNullable(streamOfEmployees).flatMap(Collection::stream).forEach(System.out::println);
}
}
15 changes: 15 additions & 0 deletions 017-streams-updates/src/main/java/StreamPredicateIterate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.stream.Stream;

public class StreamPredicateIterate {

public static void main(String[] args) {
System.out.println("Remaining days of the week:");
Stream.iterate(
LocalDate.now().getDayOfWeek().getValue(),
d -> d <= DayOfWeek.SUNDAY.getValue(),
d -> ++d)
.forEach(d -> System.out.println(DayOfWeek.of(d)));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Teeing

```java
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<module>014-scanner</module>
<module>015-record-projections</module>
<module>016-scanner-tokenize</module>
<module>xxx-streams-updates</module>
<module>017-streams-updates</module>
<module>xxx-new-api-javadoc</module>
<module>xxx-transfer-jmx</module>
<module>xxx-teeing</module>
Expand Down
4 changes: 0 additions & 4 deletions xxx-streams-updates/iterator.md

This file was deleted.

12 changes: 0 additions & 12 deletions xxx-streams-updates/overview.md

This file was deleted.

23 changes: 0 additions & 23 deletions xxx-streams-updates/script.md

This file was deleted.

11 changes: 0 additions & 11 deletions xxx-streams-updates/src/main/java/StreamIterator.java

This file was deleted.

23 changes: 0 additions & 23 deletions xxx-streams-updates/src/main/java/StreamOfNullable.java

This file was deleted.

0 comments on commit f763b60

Please sign in to comment.