Friday, November 9, 2018

Java 8 - Streams - Intermediate Operations

Greetings!

Intermediate operations are used to transform the stream into another. These operations can be chained and merge internally as necessary. Lazily process and process only when a terminal operations is called.

Ex:-
map, flatMap, filter, distinct, limit, skip, sorted are intermediate operations

  • Note that forEach is a terminal operation which close the stream.

map()

Convert the iteration element into another.


public static void main(String[] args) {
    List<String> countries = Arrays.asList("Sri Lanka", "India", "Pakistan", "England", "Australia");
    countries.stream().map(String::toUpperCase).forEach(System.out::println);
    countries.stream().map(country -> {
        // create any object and return
        int length = country.length();
        return "Length of ".concat(country).concat(" string is ").concat(String.valueOf(length));
    }).forEach(System.out::println);
}



filter()

Filter the stream elements by given condition.


public static void main(String[] args) {
    List<String> countries = Arrays.asList(
            "Sri Lanka", "India", 
            "Pakistan", "England", 
            "Australia", "South Africa");
    // filter country names started with S
    countries.stream().filter(country -> country.startsWith("S")).forEach(System.out::println);
    
    // filter country names length greater than 5
    countries.stream().filter(country -> country.length() > 5).forEach(System.out::println);
}



distinct()

Select distinct values from the stream.


public static void main(String[] args) {
    List<String> countries = Arrays.asList(
            "Sri Lanka", "India", 
            "Pakistan", "England", "Sri Lanka",
            "Australia", "South Africa", "Pakistan");

    countries.stream().distinct().forEach(System.out::println);
}



limit()

Limit the stream by given value.


public static void main(String[] args) {
    List<String> countries = Arrays.asList(
            "Sri Lanka", "India", 
            "Pakistan", "England",
            "Australia", "South Africa");

    // select only first 3 countries
    countries.stream().limit(3).forEach(System.out::println);
}


skip()

Skip n number of values from the stream.


public static void main(String[] args) {
    List<String> countries = Arrays.asList(
            "Sri Lanka", "India", 
            "Pakistan", "England",
            "Australia", "South Africa");

    // skip first 3 countries
    countries.stream().skip(3).forEach(System.out::println);
}


sorted()

Sort the elements in the stream by given condition.


public static void main(String[] args) {
    List<String> countries = Arrays.asList(
            "Sri Lanka", "India", 
            "Pakistan", "England",
            "Australia", "South Africa");

    // sort by natural order
    countries.stream().sorted().forEach(System.out::println);
    
    // sort by string length
    countries.stream().sorted((first, second) -> Integer.compare(first.length(), second.length()))
                        .forEach(System.out::println);
}




No comments:

Post a Comment