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:

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) {
    Convert the list to a stream
    Apply Integer.intValue() to each Integer object in the stream.
    Convert to array (of int values)

3) Sort a List of Comparable objects

public List<Integer> sort(List<Integer> list) {

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) {
               .sorted((a,b) -> b-a)

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 -> 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) {
               .filter(x -> x.startsWith("a"))

b) Select all integers greater than 1000

public List<Integer> filterIntegers(List<Integer> list) {
               .filter(x -> x > 1000)

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 *