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

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

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

Find the Second Largest Number in an Array

Problem Given an integer array, find the second largest number in the array. Solution The optimal time is linear to the length of the array N. We can iterate over the array, whenever the largest elements get updated, its current value becomes the second largest. public static int getSecondLargest(int[] arr){ int first = Integer.MIN_VALUE; int … Read more