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

5 Different Syntax of Lambda Expression

1. Standard Syntax Consider this example: String[] arr = {"program", "creek", "is", "a", "java", "site"}; Arrays.sort(arr, (String m, String n) -> Integer.compare(m.length(), n.length())); System.out.println(Arrays.toString(arr));String[] arr = {"program", "creek", "is", "a", "java", "site"}; Arrays.sort(arr, (String m, String n) -> Integer.compare(m.length(), n.length())); System.out.println(Arrays.toString(arr)); The standard syntax of a lambda expression consists of the following: A comma-separated list … Read more

Why do we need Lambda in Java?

A lambda expression is a block of code that can be passed around to execute. It is a common feature for some programming languages, such as Lisp, Python, Scala, etc. But before Java 8, we can not do the same in Java. If we want a block of code to be executed, we need to … Read more

How Function Interfaces Work in Java 8?

In this post, I will use a simple example to illustrate how function interfaces work in Java 8. 1. Simple Example of Stream.filter() The following code can be used to filter a list of strings by specifying the string’s length. package com.programcreek.java8.stream;   import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream;   public class Java8Filter … Read more