Filtering Data Before and After a Condition with Streams - Java Lambda Stream

Java examples for Lambda Stream:Stream

Description

Filtering Data Before and After a Condition with Streams

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 {
    
    public static void main(String[] args){
        List<String> myLangs = Arrays.asList("a","b","c","d","e","","f");
        System.out.println("Collection Data: " + myLangs);
        
        takeWhileExample();
        dropWhileExample();
    }
    
    public static void takeWhileExample(){
        Stream.of("Jython","Groovy","Scala","JRuby","Java","","Kotlin")
                .takeWhile(s -> !s.contains("Java"))
                .forEach(System.out::println);
       
    }
    
    public static void dropWhileExample(){
        Stream.of("Jython","Groovy","Scala","JRuby","Java","","Kotlin")
                .dropWhile(s -> !s.contains("Java"))
                .forEach(System.out::println);
       
    }
}

Related Tutorials