Skip to content

Commit

Permalink
Challenge 58.
Browse files Browse the repository at this point in the history
Stream intermediate operations.
peek vs forEach
  • Loading branch information
JavaZakariae committed Dec 3, 2019
1 parent 6275dee commit c97cfb6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/challenge51_60/Challenge_58.java
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);
}
}

0 comments on commit c97cfb6

Please sign in to comment.