Skip to content

Instantly share code, notes, and snippets.

@ucguy4u
Last active September 19, 2020 16:23
Show Gist options
  • Save ucguy4u/b42332c4f21d58bc52471144c85f3e58 to your computer and use it in GitHub Desktop.
Save ucguy4u/b42332c4f21d58bc52471144c85f3e58 to your computer and use it in GitHub Desktop.
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