Signup/Sign In

Stream API Improvement

Java added some new methods into Stream API in Java 9 version. These methods return a stream of collection, for example, we want to get a stream of the collection with a specified predicate. We will discuss these with the help of examples, but first, let's have a look at what Java 9 added (methods) into Stream API.

  • takeWhile()
  • dropWhile()
  • iterate()
  • ofNullable()

Java takeWhile() Method

This method is used to get a stream consisting of a subset of elements taken from this stream that match the given predicate.

default Stream<T> takeWhile(Predicate<super T> predicate)

Example: Takewhile() Method

This method takes the elements till the specified predicate. For example, if we provide predicate to 10, then it takes elements till 10 and stops further traversing.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main { 
	public static void main(String[] args){  
		 List<Integer> list   
	        = Stream.of(20,15,30,10,50)  
	                .takeWhile(i -> (i>10)).collect(Collectors.toList());     
	    System.out.println(list);  
	}
}


[20, 15, 30]

Example 2: takeWhile() Method

In this example, we have a stream of character and getting a subset of the stream till the 'h' and collecting into a list.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main { 
	public static void main(String[] args){  
		 List<String> list   
	        = Stream.of("a", "b", "c", "d", "e", "f", "g", "h", "i")  
	                .takeWhile(a->!a.equals("h")).collect(Collectors.toList());     
	    System.out.println(list);  
	}
}


[a, b, c, d, e, f, g]

Java dropWhile() Method

This method is used to get a subset of stream after the specified predicate. It drops all the elements accrued before the specified elements.

In other words, we can say that it returns a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate.

default Stream<T> dropWhile(Predicate<super T> predicate)

Example: dropWhile() Method

In this example, we are getting a list of elements that occurred after the 'h' by using the dropWhile() method.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main { 
	public static void main(String[] args){  
		 List<String> list   
	        = Stream.of("a", "b", "c", "d", "e", "f", "g", "h", "i")  
	                .dropWhile(a->!a.equals("h")).collect(Collectors.toList());     
	    System.out.println(list);  
	}
}


[h, i]

Example 2: dropWhile()

public class Main { 
	public static void main(String[] args){  
		 List<Integer> list   
	        = Stream.of(20,15,30,10,50)  
	                .dropWhile(i -> (i>10)).collect(Collectors.toList());     
	    System.out.println(list);  
	}
}


[10, 50]

Stream: New Iterate Method

A new overloaded method iterate is added to the Java 9 stream interface. This method allows us to iterate stream elements till the specified condition.

It takes three arguments, seed, hasNext, and next.

static <T> Stream<T> iterate(T seed, Predicate<super T> hasNext, UnaryOperator<T> next)

Example:

In this example, we are collecting odd elements into a list after iterating through the iterate() method.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main { 
	public static void main(String[] args){    
	     Stream<Integer> listN = Stream.iterate(1, i->i<10, i->i+2);
	     List<Integer> list = listN.collect(Collectors.toList());
	     System.out.println(list);   
	}
}


[1, 3, 5, 7, 9]



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.