Languages
[Edit]
EN

Java 9 - Stream API improvements

2 points
Created by:
Tomeriko
383

In java 9 there are 4 main improvements of Stream class:

Stream.takeWhile()
Stream.dropWhile()
Stream.iterate()
Stream.ofNullable()


1. Stream.takeWhile()

Example:

// Stream.takeWhile - take A, B, C and stop when string is empty
Stream<String> stream = Stream.of("A", "B", "C", "", "E", "F", "G")
        .takeWhile(s -> !s.isEmpty());
System.out.println(stream.collect(Collectors.toList())); // [A, B, C]

Stream<String> stream2 = Stream.of("A", "B", "C", "", "E", "", "G")
        .takeWhile(s -> !s.isEmpty());
System.out.println(stream2.collect(Collectors.toList())); // [A, B, C]


2. Stream.dropWhile()

Example:

// Stream.dropWhile - drop all values until there
// is empty string, until now take all values
// including 1st empty string
// in this case drop A, B, C and take the rest - "", E, F, G
Stream<String> stream = Stream.of("A", "B", "C", "", "E", "F", "G")
        .dropWhile(s -> !s.isEmpty());

List<String> resultList = stream.collect(Collectors.toList());
System.out.println(resultList.size()); // 4
System.out.println(resultList); // [, E, F, G]

// in this case take the rest - "", E, "", G
Stream<String> stream2 = Stream.of("A", "B", "C", "", "E", "", "G")
        .dropWhile(s -> !s.isEmpty());

List<String> resultList2 = stream2.collect(Collectors.toList());
System.out.println(resultList2.size()); // 4
System.out.println(resultList2); // [, E, , G]


3. Stream.iterate()

Example:

// Stream.iterate - 2 x arguments
Stream<String> stream = Stream.iterate("", str -> str + "s")
        .takeWhile(s -> s.length() < 5);
// [, s, ss, sss, ssss]
System.out.println(stream.collect(Collectors.toList()));

// Stream.iterate - 3 x arguments
Stream.iterate(0, i -> i < 5, i -> i + 1)
        .forEach(i -> System.out.print(i + ", ")); // 0, 1, 2, 3, 4,

// equivalent method with for loop
for (int i = 0; i < 5; ++i) {
    System.out.print(i + ", "); // 0, 1, 2, 3, 4,
}


4. Stream.ofNullable()

Example 1:

long count1 = Stream.ofNullable(123).count();
System.out.println(count1); // 1

long count2 = Stream.ofNullable(null).count();
System.out.println(count2); // 2

Example 2:
Before java 9 and with java 9 Stream.ofNullable example:

Map<String, Integer> letterToNoMap = new HashMap<>();
letterToNoMap.put("A", 1);
letterToNoMap.put("B", 2);
letterToNoMap.put("C", null);
letterToNoMap.put("D", 4);

List<String> letters = Arrays.asList("A", "B", "C", "D");

// before java 9 - Stream.ofNullable
// we need to check if no == null because if we don't
// the null will be added to the result list
List<Integer> collect1 = letters.stream()
        .flatMap(letter -> {
            Integer no = letterToNoMap.get(letter);
            if (no == null) {
                return Stream.empty();
            }
            return Stream.of(no);
        })
        .collect(Collectors.toList());

System.out.println(collect1); // [1, 2, 4]

// with java 9 - Stream.ofNullable
List<Integer> collect2 = letters.stream()
        .flatMap(letter ->
                Stream.ofNullable(letterToNoMap.get(letter)))
        .collect(Collectors.toList());

System.out.println(collect2); // [1, 2, 4]


NOTE:
compiled with jdk 9.0.1


Java 9 stream API useful links:

https://docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html
https://docs.oracle.com/javase/9/docs/api/java/util/stream/package-summary.html
https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join