Skip to content

Instantly share code, notes, and snippets.

@ejwinter
Last active March 5, 2017 20:17
Show Gist options
  • Save ejwinter/b189fa059cb3e16918095c787a54b866 to your computer and use it in GitHub Desktop.
Save ejwinter/b189fa059cb3e16918095c787a54b866 to your computer and use it in GitHub Desktop.
import concurrent.ParallelStreams;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Created by winte on 3/5/17.
* based on https://github.com/kousen/refactoring_java_8
*/
public class FuntionalFun {
public static void main(String[] args) {
new Thread(() -> System.out.println("hi")).start();
List<String> list = Arrays.asList("A", "BA", "BB", "C");
list.stream()
.forEach(s -> System.out.println(s));
System.out.println("----");
Stream.of("A", "B", "C")
.forEach(s -> System.out.println(s));
System.out.println("----");
Stream.of("A", "B", "C")
.forEach(System.out::println);
System.out.println("----");
//or there is a foreach already
list
.forEach(System.out::println);
System.out.println("----");
String values = list.stream()
.filter(s -> s.startsWith("B"))
.collect(Collectors.joining(","));
System.out.println(values);
//example of using a block lambda
System.out.println("----");
System.out.println(list.stream()
.filter(s -> {
System.out.println("in filter:" + s + " - " + s.startsWith("B"));
return s.startsWith("B");
}).collect(Collectors.joining(",")));
System.out.println("----");
IntStream.rangeClosed(1, 9).forEach(System.out::println);
System.out.println("----");
//demonstrating lazy streams, even though the range is very high it short circuits at filter
//find first is a short circuiting terminating operation
int firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 2_000_000_000)
.map(n -> n * 2)
.filter(n -> n % 3 == 0)
.findFirst()
.orElse(0); //return an int and not an OptionalInt
System.out.println(firstEvenDoubleDivBy3);
System.out.println("----");
//make parallel and use methods since we added complexity
//parallel automatically uses as many threads as you have cores and splits up the stream
OptionalInt firstEvenDoubleDivBy3_2 = IntStream.rangeClosed(100, 2_000_000_000)
.parallel()
.map(UseRunnable::multByTwo)
.filter(UseRunnable::modByThree)
.findAny(); // finds the first result from all threads
System.out.println(firstEvenDoubleDivBy3_2.getAsInt());
System.out.println("-----");
System.out.println(IntStream.rangeClosed(1, 100_000_000)
.parallel() //int streams are very fast so this is not the best case
.sum());
System.out.println("-----");
// when we have a regular stream we have to use reduce, sum() on intstream is just a convenience
System.out.println(Stream.of(1, 2, 3, 4, 5)
.reduce(Integer::sum));
System.out.println("-----");
//use your own accumulator
System.out.println(Stream.of(1, 2, 3, 4, 5)
.reduce(1, (accumulator, value) -> {
System.out.println("acc: " + accumulator + ", value: " + value);
return accumulator * value;
}));
System.out.println("-----");
System.out.println(Arrays.stream("This is a great string of values!".split(" "))
.sorted(String::compareToIgnoreCase) //sort the stream
.collect(Collectors.toList())); //turn the stream into a list
}
public static int multByTwo(int n) {
System.out.printf("Inside multByTwo with arg %d%n", n);
return n * 2;
}
public static boolean modByThree(int n) {
System.out.printf("Inside divByThree with arg %d%n", n);
return n % 3 == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment