Stream.dropWhile() doesn't return correct value in two different values

java 8 stream dropwhile
stream ofnullable
java stream drop
stream iterate
java stream limit
java stream collect
dropwhile scala
java stream count

I am trying to learn new features in Java-9 I come to know about the dropWhile method of Stream but it is returning different values in two different scenarios. Here is my code

package src.module;

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

public class Test {

    public static void main(String[] args) {

        String s[] = new String[3];
        s[0] = "Hello";
        s[1] = "";
        s[2] = "World";
        List<String> value = Stream.of(s).dropWhile(a -> a.isEmpty()).collect(Collectors.toList());

        System.out.println(value);

        List<String> values = Stream.of("a", "b", "c", "", "e", "f").dropWhile(d -> !d.isEmpty())
                .collect(Collectors.toList());
        System.out.println(values);

    }
}

Here is the answer what I am getting

[Hello, , World]
[, e, f]

What I think in first condition it should print [,World]. Thanks in advance.

The dropWhile method, introduced in Java 9, will remove the longest starting set of elements that match the predicate.

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

Because your condition is that the item is empty, and the first item is not empty, nothing is removed, leaving ["Hello", "", "World"] intact.

At the end, when you call dropWhile with the opposite condition, is not empty, the first 3 items match and are removed, leaving ["", "e", "f"], which are the remaining items.

This is the expected behavior.

Stream dropWhile() method in Java with examples, In the first snippet, the first element of the Stream doesn't satisfy a -> a.isEmpty() , so no elements are dropped. In the second snippet, the first 3  I am trying to learn new features in Java-9 I come to know about the dropWhile method of Stream but it is returning different values in two different scenarios. Here is my code package src.module;

Your first condition is saying to drop items until a non-empty item is found The second condition says to drop items until an empty item is found. Add a '!' to your first condition to get your predicted result.

Stream (Java SE 9 & JDK 9 ), Table of Contents Limiting Stream with takeWhile() and dropWhile() methods Overloaded The dropWhile method does the opposite of takeWhile method. Stream.dropWhile starts at the beginning of the stream and applies a predicate to each item, one by one. It does not start returning items in a new stream until it reaches an item that does not meet the predicate. Then it stops checking elements against the predicate and returns every item in the stream from that point on:

The Javadoc of dropWhile states:

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

In the first snippet, the first element of the Stream doesn't satisfy a -> a.isEmpty(), so no elements are dropped.

In the second snippet, the first 3 elements in the Stream satisfy d -> !d.isEmpty(), so these 3 elements are dropped, leaving "", "e" & "f".

Stream.dropWhile() doesn't return correct value in two different , A quick and practical guide to Java 9 Stream API additions. takeWhile(s -> s.​length() < 10 ); immediately following the last element of the sequence does not match the given predicate; a Stream's subset is After introducing these key terms, we can easily comprehend another new dropWhile operation. 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.

In order to better understand the algorithm, you can try to replace a Stream version:

List<String> value = Stream.of(s).dropWhile(String::isEmpty).collect(Collectors.toList());

with a classic for loop:

List<String> value = new ArrayList<>();
boolean dropping = true;

for (int i = 0; i < s.length; i++) {
    String str = s[i];
    if (dropping) {
        if (str.isEmpty()) {
            continue;
        }
        dropping = false;
    }
    value.add(str);
}

Java 9 Stream API Improvements, Note: When the stream is unordered, the dropWhile() drops all the elements until the given predicate fails, this method does not check further elements of the  Java Stream takeWhile. it will not consider elements once predicate returns false. It used to discard ending elements from Stream. If this stream is ordered then the longest prefix is a contiguous sequence of elements of this stream that match the given predicate.

Java 9 Stream API improvements, dropWhile has been added since Java 9. It is the opposite of takeWhile and drops elements from the stream until the first element which does  If you notice above, 17 & 19 are also matching the predicate but because 16 is not matching, therefore, the returning stream won’t consider 17 and 19 and just cut off at the failing element.

Java 9, This article is about only a small piece of a new Stream API. Bad news is that Java 8 does not have dropWhile() and takeWhile() methods. Last Updated on September 6, 2019. This post demonstrates how to use the new methods – takeWhile and dropWhile – of the Stream interface in Java 9 Jigsaw.We’ll touch briefly on Stream.filter(Predicate p) just to get comfortable before trying out the new methods.

Streams API New Features after Java 8, The method Stream<T> dropWhile(Predicate<? super T> predicate) drops super T> predicate) performs the opposite action on a stream that dropWhile() does. In an ordered stream, dropWhile removes the longest contiguous sequence of elements that match the given predicate. In this example we’re dropping even numbers. 2 , 4 , 6 and 8 are removed because applying the predicate on them returns true .

Comments
  • what my doubt is in the first condition it then returns the Hello only and the left two value should be dropped. Isn't it?
  • what my doubt is in the first condition it then returns the Hello only and the left two value should be dropped. Isn't it?
  • @HarshKumrawat nothing should be dropped, since the "longest prefix of elements that match the given predicate" is empty.
  • what my doubt is in the first condition it then returns the Hello only and the left two value should be dropped. Isn't it?
  • The 'dropWhile' runs as long as the provided condition is true. It stops when the condition is false. In the first loop, the condition is false from the start. In the second loop, it is true for 2 elements of the stream.
  • @HarshKumrawat What he's saying is, you put a.isEmpty() when you wanted !a.isEmpty(). That's why the output isn't what you expected.
  • what my doubt is in the first condition it then returns the Hello only and the left two value should be dropped. Isn't it?
  • @HarshKumrawat Nothing should be dropped, since the "longest prefix of elements that match the given predicate" is empty.
  • As I am learning the stream feature so I only want to use Stream methods.Thanks for the answer
  • This is just a view from a different angle :) ... Keep learning!