Fundamentals 7 min read

What New APIs Does Java 9 Bring? Collections, Streams, Optional, and More

Java 9 introduces several notable enhancements—including immutable collection factories, expanded Stream operations like ofNullable, iterate, takeWhile, dropWhile, new Optional methods, simplified try‑with‑resources, private interface methods, a modern HttpClient, and preliminary Flow support—each illustrated with concise code examples for developers transitioning from earlier Java versions.

Programmer DD
Programmer DD
Programmer DD
What New APIs Does Java 9 Bring? Collections, Streams, Optional, and More

Java 9

Java 9’s biggest changes are the introduction of JShell and modularity, but this article focuses on practical API updates.

New collection factory methods

Guava provides static factory methods for collections; Java 9 adds built‑in immutable factories:

List<Integer> integers = List.of(1, 2, 3, 4);
Set<Integer> integerSet = Set.of(1, 2, 3);
Map<String, String> hello = Map.of("hello","world","hi","java");
Note: Collections created with these factories are immutable.

Stream extensions

The Stream API, introduced in Java 8, receives several enhancements in Java 9.

ofNullable

Stream<T> ofNullable(T t)

returns a single‑element stream if the argument is non‑null, otherwise an empty stream.

iterate

Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)

This overload allows generating a finite stream with a termination predicate.

Example:

Stream.iterate(0, i -> i < 5, i -> i + 1)
      .forEach(System.out::println);

takeWhile

Stream.takeWhile(Predicate)

consumes elements while the predicate is true, stopping at the first false.

Stream.of(1,2,3,4,2,5)
      .takeWhile(x -> x < 4)
      .forEach(System.out::println);

dropWhile

Stream.dropWhile(Predicate)

discards elements while the predicate is true and returns the remaining elements.

Stream.of(1,2,3,4,2,5)
      .dropWhile(x -> x < 4)
      .forEach(System.out::println);
Unlike filter , these methods affect the stream flow differently.

Optional extensions

Three useful methods were added: stream() – converts an Optional to a Stream.

ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)

– handles present and absent cases. or(Supplier<? extends Optional<? extends T>> supplier) – provides an alternative Optional when empty.

try‑with‑resources simplification

Since Java 7, resources implementing AutoCloseable or Closeable could be managed with try‑with‑resources. Java 9 allows using already‑declared variables:

BufferedInputStream in1 = new BufferedInputStream(System.in);
BufferedInputStream in2 = new BufferedInputStream(System.in);
try (in1; in2) {
    // do something
} catch (IOException e) {
    e.printStackTrace();
}

Private interface methods

public interface Catable {
    private void doSomething() {
        // implementation
    }
}

New HttpClient API

A modern HTTP client supporting HTTP/2 and WebSocket replaces the old HttpURLConnection:

HttpRequest request = HttpRequest.newBuilder(newURI)
    .header("Content-Type","*/*")
    .GET()
    .build();
HttpClient client = HttpClient.newBuilder()
    .connectTimeout(Duration.of(10, ChronoUnit.SECONDS))
    .version(HttpClient.Version.HTTP_2)
    .build();

Flow (reactive streams) support

Java 9 introduces the java.util.concurrent.Flow interfaces, aligning with the Reactive Streams specification.

Summary

For most developers, the most useful Java 9 features are the immutable collection factories and the streamlined try‑with‑resources syntax; the remaining enhancements are valuable when you are already comfortable with Java 8.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

try-with-resourcesoptionalStream APIHttpClientJava 9
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.