-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
19 changed files
with
115 additions
and
81 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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 @@ | ||
# iterate with Predicate | ||
|
||
```java | ||
``` | ||
|
||
## Output |
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,3 +1,5 @@ | ||
# not | ||
|
||
```java | ||
``` | ||
|
||
|
2 changes: 2 additions & 0 deletions
2
xxx-streams-updates/ofNullable.md → 017-streams-updates/ofNullable.md
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,3 +1,5 @@ | ||
# ofNullable | ||
|
||
```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
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,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! |
File renamed without changes.
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,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); | ||
} | ||
|
||
} |
11 changes: 7 additions & 4 deletions
11
...s-updates/src/main/java/PredicateNot.java → ...ates/src/main/java/PredicateNotJava8.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,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); | ||
} | ||
|
||
} |
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,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
15
017-streams-updates/src/main/java/StreamPredicateIterate.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 |
---|---|---|
@@ -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))); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
xxx-streams-updates/teeing.md → 017-streams-updates/teeing.md
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,3 +1,5 @@ | ||
## Teeing | ||
|
||
```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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.