Friday, September 29, 2017

Java 9 Streams : dropWhile()

In Java 9, comes with a few good additions to the Stream API. For more information on the Java 8 Streams, go to the Java 8 Page. The following new methods were added to the Java 9 Stream API.
  1. dropWhile
  2. dropWhile
  3. iterate
  4. ofNullable
In this post we will take a look at the dropWhile() method.

dropWhile() method

In Stream API, dropWhile() Method takes a Predicate(function returning true or false) as an argument, and drops the set of elements of the stream that match the Predicate and returns the rest of the elements. However, this behavior varies between ordered and unordered streams.
Stream dropWhile(Predicate predicate)
In all the examples below, We will use the following list as a starting point, unless mentioned otherwise.
List<String> list = List.of("returns", "if", "this", "stream", "is", "ordered,", "stream", "consisting", "of", "the",
  "longest", "prefix", "of", "elements", "taken", "from", "this", "stream", "that", "match", "the", "given",
  "predicate");
As you can see, the sorted version of the list will look like this
consisting, elements, from, given, if, is, longest, match, of, of, ordered,, predicate, prefix, returns, stream, stream, stream, taken, that, the, the, this, this

For Ordered Stream

From the JavadocIf this stream is ordered then the longest prefix is a contiguous sequence of elements of this stream that match the given predicate. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate. This effectively means that if for an ordered list, dropWhile() will return all values if the first element doesn't match the predicate. You can see this behavior in the following example where we Predicate is to check if the length of a string is less than 4. Since the first element of the sorted stream "consists" is longer than 4 characters, we get back all elements of the Stream
  list
   .stream()
   .sorted()
   .dropWhile(x -> x.length() < 4)
   .forEach(System.out::println);
However, if the first element of the stream matches the predicate, then it will drop the first few elements of the Stream that match the predicate, and return the remaning elements. As soon as the predicate returns false, dropWhile() stops, even if there are any subsequent elements that match the predicate further downstream. In the following example, we check for strings length greater than 2, which will return "if, is, longest, match, of, of, ordered,, predicate, prefix, returns, stream, stream, stream, taken, that, the, the, this, this", which is the list of elements starting with the first element "if" that matches the predicate.
list
   .stream()
   .sorted()
   .dropWhile(x -> x.length() >2)
   .forEach(x-> System.out.print(x + ", "));
 
Output:
"if, is, longest, match, of, of, ordered,, predicate, prefix, returns, stream, stream, stream, taken, that, the, the, this, this, 
Another example
list
   .stream()
   .sorted()
   .dropWhile(x -> x.contains("c"))
   .forEach(x-> System.out.print(x + ", "));
   
Output:
elements, from, given, if, is, longest, match, of, of, ordered,, predicate, prefix, returns, stream, stream, stream, taken, that, the, the, this, this, 

For Unordered Stream

From the JavadocIf this stream is unordered, and some (but not all) elements of this stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to take any subset of matching elements (which includes the empty set).
list
  .stream()
  .dropWhile(x -> x.contains("t"))
  .forEach(x-> System.out.print(x + ", "));
  Output:
 if, this, stream, is, ordered,, stream, consisting, of, the, longest, prefix, of, elements, taken, from, this, stream, that, match, the, given, predicate, 
The following Stream of number where we check for number less than 7,
Stream.of(1,5,3,2,4,9,7)
   .dropWhile(x->x <7)
   .forEach(System.out::println);
   
Output:
9
7

Here is the full code

package com.aoj.java9.collections;

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

public class Java9StreamsDropWhile {
 public static void main(String[] args) {
  List<String> list = List.of("returns", "if", "this", "stream", "is", "ordered,", "stream", "consisting", "of", "the",
    "longest", "prefix", "of", "elements", "taken", "from", "this", "stream", "that", "match", "the", "given",
    "predicate");

  list.stream().sorted().forEach(x-> System.out.print(x + ", "));
  System.out.println();
  list
   .stream()
   .sorted()
   .dropWhile(x -> x.length() >2)
   .forEach(x-> System.out.print(x + ", "));

   list
   .stream()
   .sorted()
   .dropWhile(x -> x.contains("c"))
   .forEach(x-> System.out.print(x + ", "));

  list
   .stream()
   .sorted()
   .dropWhile(x -> x.length() < 4)
   .forEach(System.out::println);

  list
  .stream()
  .dropWhile(x -> x.contains("t"))
  .forEach(x-> System.out.print(x + ", "));
  
  Stream.of(1,5,3,2,4,9,7)
   .dropWhile(x->x <7)
   .forEach(System.out::println);
 }

}

No comments:

Post a Comment

Popular Posts