Last active
September 19, 2020 16:23
-
-
Save ucguy4u/b42332c4f21d58bc52471144c85f3e58 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.ucguy4u.functional.java; | |
import java.util.Arrays; | |
import java.util.List; | |
import static java.util.stream.Collectors.toList; | |
import java.util.stream.Stream; | |
/** | |
* | |
* @author Chauhanuday | |
*/ | |
public class Lambdas_05 { | |
public static void main(String[] args) { | |
Arrays.asList("red", "green", "blue").stream().sorted().findFirst().ifPresent(System.out::println); | |
// example of Stream.of with a filter | |
Stream.of("apple", "pear", "banana", "cherry", "apricot").filter(fruit -> { | |
// System.out.println("filter: " + fruit); | |
return fruit.startsWith("a"); // predicate | |
}) | |
// if the foreach is removed, nothing will print, | |
// the foreach makes it a terminal event | |
.forEach(fruit -> System.out.println("Starts with A: " + fruit)); | |
// using a stream and map operation to create a list of words in caps | |
List<String> collected = Stream.of("Java", " Rocks").map(string -> string.toUpperCase()).collect(toList()); | |
System.out.println(collected.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment