Java Stream How to - Filter twice for null String value and string length








The following code shows how to filter twice for null String value and string length.

Example

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
// w w  w.  jav  a 2 s .  c  o  m
public class Main {

  public static void main(String[] args)  {
    List<String> liste = Arrays.asList("XML", "HTML", "CSS", null);

    liste.parallelStream()// Parallel stuff uses Fork-Join
            .filter(e -> (!Objects.equals(e, null))) // lazy & parallel
            .filter(e -> (e.length() > 3)) // lazy & parallel
            .forEach(e -> {   // sequential & eagerly
                System.out.println("Bigger length than 3 in List: " + e);
            });

  }
}

The code above generates the following result.