-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stream intermediate operations. peek vs forEach
- Loading branch information
1 parent
6275dee
commit c97cfb6
Showing
1 changed file
with
26 additions
and
0 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,26 @@ | ||
package challenge51_60; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* unlike forEach, peek is an intermediate operation. | ||
* peek return a stream of T, forEach return void, both | ||
* receive a consumer lambda expression as an input argument. | ||
* | ||
*/ | ||
public class Challenge_58 { | ||
|
||
public static void main( String[] args ) { | ||
|
||
List<String> list = List.of("Yoda", "Luke", "Anakin", "Obi Wan", "Luke"); | ||
list.stream() | ||
.skip(1) | ||
.filter(jedi->jedi.startsWith("Lu")) | ||
.limit(4) | ||
.distinct() | ||
.map(String::length) | ||
.flatMap(jedi-> Stream.of(jedi)) | ||
.peek(System.out::println); | ||
} | ||
} |