×
☰ See All Chapters

Stream iterate in Java 9

Java Stream really missed to provide the index while iterating. While Stream provides greater support, still we would need to use normal for loop when wanted to get the current index or the current iteration count. As an example when you want separate the list into two list with odd even index, we must use normal for loop to get the index while iterating.

package com.java4coding.test;

 

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

import java.util.stream.Stream;

 

public class Test {

        public static void main(String[] args) {

                List<Integer> list = Stream.of(1, 6, 7, 4, 9, 6, 9, 1, 2, 3).collect(Collectors.toList());

                List<Integer> oddIndexList = new ArrayList<>();

                List<Integer> evenIndexList = new ArrayList<>();

 

                for (int x = 0; x < list.size(); x++) {

                        if (x % 2 == 0) {

                                evenIndexList.add(list.get(x));

                        } else {

                                oddIndexList.add(list.get(x));

                        }

                }

 

                System.out.println(evenIndexList);

                System.out.println(oddIndexList);

        }

}

Java 9 introduced below two stream iterate methods to solve above problem and we can iterate the streams same like in regular for loop.

Modifier and Type

Method

Description

static <T> Stream<T>

iterate(initial_element; predicate_expression; update_statement)

Returns a sequential ordered Stream produced by iterative application of the given update_statement to an initial element, conditioned on satisfying the given predicate_expression.

  1. initial_element - the initial element. 

  2. predicate_expression - a predicate to apply to elements to determine when the stream must terminate. 

  3. update_statement - a function to be applied to the previous element to produce a new element. 

static <T> Stream<T>

iterate( initial_element, update_statement)

Returns an infinite sequential ordered Stream produced by iterative application of an update_statement to an initial_element. Be careful when using this, it generates infinite Stream.

  1. initial_element - the initial element 

  2. update_statement - a function to be applied to the previous element to produce a new element. 

Example

package com.java4coding.test;

 

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

import java.util.stream.Stream;

 

public class Test {

        public static void main(String[] args) {

                List<Integer> list = Stream.of(1, 6, 7, 4, 9, 6, 9, 1, 2, 3).collect(Collectors.toList());

                List<Integer> oddIndexList = new ArrayList<>();

                List<Integer> evenIndexList = new ArrayList<>();

                Stream.iterate(0, x -> x < list.size(), x -> ++x).forEach(x -> {

                        if (x%2 == 0) {

                                evenIndexList.add(list.get(x));

                        } else {

                                oddIndexList.add(list.get(x));

                        }

                });

               

                System.out.println(evenIndexList);

                System.out.println(oddIndexList);

        }

}

 

Note: increment/decrement operators should be prefixed, postfix (var++,  var--) will result in infinite streams.

 


All Chapters
Author