Java Fork/Join Framework examples

What is Fork/Join? Read this Java Fork/Join paper by Doug Lea The fork/join framework is available since Java 7, to make it easier to write parallel programs. We can implement the fork/join framework by extending either RecursiveTask or RecursiveAction 1. Fork/Join – RecursiveTask A fork join example to sum all the numbers from a range. …

Read more

Java Fibonacci examples

Fibonacci number – Every number after the first two is the sum of the two preceding. Few Java examples to find the Fibonacci numbers. 1. Java 8 stream 1.1 In Java 8, we can use Stream.iterate to generate Fibonacci numbers like this : Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]}) .limit(10) .forEach(x …

Read more

Java 8 Stream.iterate examples

In Java 8, we can use Stream.iterate to create stream values on demand, so called infinite stream. 1. Stream.iterate 1.1 Stream of 0 – 9 //Stream.iterate(initial value, next value) Stream.iterate(0, n -> n + 1) .limit(10) .forEach(x -> System.out.println(x)); Output 0 1 2 3 4 5 6 7 8 9 1.2 Stream of odd numbers …

Read more