Java Stream filter data before a condition

Description

Java Stream filter data before a condition

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    /* w  ww  .jav a2s.c  o m*/
    public static void main(String[] args){
        List<String> myLangs = Arrays.asList("CSS","HTML","Java");
        System.out.println("Collection Data: " + myLangs);
        
        Stream.of("CSS","HTML","Java")
                .takeWhile(s -> !s.contains("Java"))
                .forEach(System.out::println);
       
        Stream.of("CSS","HTML","Java")
                .dropWhile(s -> !s.contains("Java"))
                .forEach(System.out::println);
       
    }
}



PreviousNext

Related