Convert Stream to Array in Java 8

To convert a Stream to an array, there is an overloaded version of toArray() method for Stream objects. The toArray(IntFunction<A[]> generator) method returns an array containing the elements of this stream, using the provided generator function to allocate the returned array. String[] stringArr = { "a", "b", "c", "d" }; Stream<String> stream = Stream.of(stringArr); String[] … Read more

Enhanced For-loop vs. forEach() in Java 8

Assuming you have the following list: List<String> list = Arrays.asList("a", "b", "c", "d", "e", "f");List<String> list = Arrays.asList("a", "b", "c", "d", "e", "f"); if you want to do something by using each element in a collection, there are two ways: 1) list.forEach(s -> System.out.println(s));list.forEach(s -> System.out.println(s)); 2) for(String s: list){ System.out.println(s); }for(String s: list){ System.out.println(s); … Read more

How to write a counter in Java 8?

Writing a counter in Java can be as simple as 2 lines. In addition to its simplicity, we can also utilize the parallel computation to increase the counter’s performance. import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.Map;   public class Java8Counter { public static void main(String[] args) { String[] arr = { "program", "creek", "program", "creek", "java", … Read more

Primitive Type Stream Examples

In addition to Stream, java.util.stream package also provide a set of primitive type Streams such as DoubleStream, IntStream and LongStream. The primitive type streams are pretty similar with Stream. In this post I will use the IntStream to illustrate how to use primitive type streams. Create an IntStream From integer array: IntStream stream = IntStream.of(2, … Read more

How to Use Optional Type in Java 8 ?

1. Correct Way to Use Optional Consider the following example: List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); list.add("perl"); list.add("c"); list.add("lisp"); list.add("c#"); Stream<String> wordStream = list.stream();   Stream<Integer> lengthStream = wordStream.map(s -> s.length()); Optional<Integer> sum = lengthStream.reduce((x, y) -> x + y); System.out.println(sum.get());List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); list.add("perl"); list.add("c"); list.add("lisp"); list.add("c#"); Stream<String> … Read more

Collect Stream Result Examples

Instead of reducing a stream to a value, we can also collect the results. We can collect results to an array, a set/list, or a map by using the Stream.collect() method. 1. Collect Stream Results to Array List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); Stream<String> wordStream = list.stream();   Stream<Integer> lengthStream = wordStream.map(s -> … Read more

Reduce Stream Examples

The Java 8 Stream API contains a set of terminal operations (such as average, sum, min, max, and count) which return one value by combining the elements of a stream. Such terminal operations are called reduction operations. In addition to those terminal operations, the JDK also provides the general-purpose reduction method – reduce(), which this … Read more

Transform Stream Using Stream.filter()

The filter() method is an intermediate operation. It returns a stream consisting of the elements of this stream that match the given condition. import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream;     public class Java8Filter<T> {   public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); list.add("lisp"); list.add("c++");   … Read more

Transform Stream using Steam.map()

The map() method is an intermediate operation. It returns a stream consisting of the results of applying the given function to each element in the stream. The following code returns a stream of Integers, which are results of applying String.length() method. import java.util.ArrayList; import java.util.List; import java.util.stream.Stream;     public class Java8Map {   public … Read more

5 Ways of Creating a Stream in Java 8

1. From Arrays String[] arr = { "program", "creek", "program", "creek", "java", "web", "program" }; stream = Stream.of(arr);String[] arr = { "program", "creek", "program", "creek", "java", "web", "program" }; stream = Stream.of(arr); stream = Stream.of("program", "creek", "program", "creek", "java", "web", "program");stream = Stream.of("program", "creek", "program", "creek", "java", "web", "program"); String[] stringArr = {"a", "b", "c", … Read more

What is Stream?

The definition of stream in Java 8 is “a sequence of elements from a source that supports aggregate operations.” Streams consume from a source such as collections, arrays, or I/O resources. Streams support the common operations from functional programing languages, such as map, filter, reduce, find, sorted, etc. A Motivational Example It is easy to … Read more

Why Stream.max(Integer::max) compiles?

Consider the following code: Stream<Integer> stream = Stream.of(1,2,3,4); int max = stream.max(Math::max).get(); System.out.println(max);Stream<Integer> stream = Stream.of(1,2,3,4); int max = stream.max(Math::max).get(); System.out.println(max); According to the Javadoc of Steam.max(), the argument for the method max() should be a Comparator. In the code above, the method reference is to static methods of the Math class. But the code … Read more

Java 8 Counter

Before Java 8, developers often think about different ways of writing a counter to count something, e.g., counting word frequency. In Java 8, you can write a counter in two simple lines! In addition you can take advantage of parallel computing. Here is Java 8 counter: package com.programcreek.java8;   import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.Map; … Read more

Retrieve a List from a Stream in Java 8

Method 1 String[] arr = { "ab", "bcd", "cdef", "defgh", "efhik", "fghijk", "ghijkl" }; Stream<String> stream = Stream.of(arr);String[] arr = { "ab", "bcd", "cdef", "defgh", "efhik", "fghijk", "ghijkl" }; Stream<String> stream = Stream.of(arr); Method 2 String[] arr = { "ab", "bcd", "cdef", "defgh", "efhik", "fghijk", "ghijkl" }; Stream<String> stream = Stream.of(arr);   ArrayList<String> list = … Read more

Concat Streams in Java 8

You may often need to concat or merge two streams. In the Stream class, there is a static method concat() that can be used for this purpose. Merge Two Streams String[] arr1 = { "a", "b", "c", "d" }; String[] arr2 = { "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); … Read more