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

Java Enum Examples

An enum in Java is just like any other class, with a predefined set of instances. Here are several examples to highlight how to use Java Enum. 1. Simple Example public enum Color { RED, YELLOW, BLUE; //each is an instance of Color }public enum Color { RED, YELLOW, BLUE; //each is an instance of … 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

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

java.util.ConcurrentModificationException

This post shows show to solve the problem of java.util.ConcurrentModificationException for ArrayList. The error message looks like the following: Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) … … The Problem You may want to iterate through an ArrayList, and delete some element under some certain condition. For example, the following code … Read more