Menu Close

Java 8 Stream cheatsheet

Java 8 introduced the Stream API, which is useful in processing collections of objects. Stream allows us to perform functional style operations (like map-reduce) on collections of objects. The goal of this article is to provide some very useful operation that are possible with the Stream API:

1) Convert int[] array to List

Very often, you might have an array of int values but you need to return a list of integers. The traditional java way of doing this is to create a new list, loop through the integer array and copy the values from the array to the list. With Stream, this process can be reduced to a single line:

import java.util.stream.*;
public List<Integer> convertToList(int[] arr) {
    return IntStream.of(arr).boxed().collect(Collectors.toList());
}
  • IntStream.of(arr)
    Creates a Stream of the int values.
  • IntStream.of(arr).boxed()
    Converts the int values to Integer objects.
  • IntStream.of(arr).boxed().collect(Collectors.toList())
    Collects the stream of Integer objects into a List.

2) Convert List to int[]

public List<Integer> convertToArray(List<Integer> list) {
    return list.stream().mapToInt(Integer::intValue).toArray();
}
  • list.stream()
    Convert the list to a stream
  • list.stream().mapToInt(Integer::intValue)
    Apply Integer.intValue() to each Integer object in the stream.
  • list.stream().mapToInt(Integer::intValue).toArray()
    Convert to array (of int values)

3) Sort a List of Comparable objects

public List<Integer> sort(List<Integer> list) {
    return list.stream().sorted().collect(Collectors.toList());
}

Note here that we do not need to call boxed, because we already have list of Integer Objects.

The purpose of boxed() is to convert a native value (eg: int) to it’s corresponding object(eg: Integer)

4) Sort a List in descending order

public List<Integer> sortDescending(List<Integer> list) {
    return list.stream()
               .sorted((a,b) -> b-a)
               .collect(Collectors.toList());
}

To sort in descending order, we use lambda expressions.

(a,b) -> b-a 

The above lambda expression returns a value greater than 0, when b > a. This tells the compiler to sort the list in descending order. Read the documentation for sorted() to know more.

5) Square each number in List

public List<Integer> squareEach(List<Integer> list) {
    return list.stream().map(i -> i*i).collect(Collectors.toList());
}

We use map and lambda expression to square each integer in the list.

6)Filter based on predicate

a) Select all strings that start with a substring

Let’s say you have a list of strings. You want to get all the strings that start with “a”. We can filter the stream to do this:

public List<String> filterStrings(List<String> list) {
    return list.stream()
               .filter(x -> x.startsWith("a"))
               .collect(Collectors.toList());
}

b) Select all integers greater than 1000

public List<Integer> filterIntegers(List<Integer> list) {
    return list.stream()
               .filter(x -> x > 1000)
               .collect(Collectors.toList());
}

These are just the most frequently used functions using the Stream API in Java 8. If you feel that something is missing, drop them in the comments below and i’ll add them here.

If you are mobile developer playing around with push notifications, try our website for testing your Apple push notifications (APNS) and Firebase Cloud Messaging(FCM) : Test APNS and FCM push notifications Online!

Leave a Reply

Your email address will not be published. Required fields are marked *