Java Stream of() Method with Examples

Ramesh Fadatare
2 min readSep 26, 2024

--

In this guide, we will learn how to use Stream.of() method in Java with practical examples and real-world use cases to better understand its functionality.

Learn Java from scratch: Learn Java Programming.

Complete Java Reference: Java API Documentation.

Table of Contents

  1. Introduction
  2. Stream.of() Method Syntax
  3. Understanding Stream.of()
  4. Examples
  • Creating a Stream from Individual Values
  • Creating a Stream from an Array

5. Real-World Use Case

6. Conclusion

Introduction

The Stream.of() method allows you to create a stream from a specified set of values or from an array. This method is particularly useful for quickly generating streams for processing.

Stream.of() Method Syntax

The syntax for the Stream.of() method is as follows:

@SafeVarargs
static <T> Stream<T> of(T... values)

Parameters:

  • values: The elements to be included in the newly created stream.

Returns:

  • A new sequential Stream containing the specified elements.

Throws:

  • This method does not throw any exceptions.

Understanding Stream.of()

The Stream.of() method is a varargs method, which means you can pass any number of arguments to create a stream. Additionally, you can pass an array to the method to create a stream from its elements.

Examples

Creating a Stream from Individual Values

To demonstrate how to create a stream from individual values using Stream.of(), we will create a stream of strings.

import java.util.stream.Stream;

public class StreamOfExample {
public static void main(String[] args) {
// Create a stream from individual values
Stream<String> stream = Stream.of("apple", "banana", "cherry");

// Print the elements of the stream
stream.forEach(System.out::println);
}
}

Output:

apple
banana
cherry

Creating a Stream from an Array

This example shows how to use Stream.of() to create a stream from an array of integers.

Example

import java.util.stream.Stream;

public class StreamOfArrayExample {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5};

// Create a stream from an array
Stream<Integer> stream = Stream.of(numbers);

// Print the elements of the stream
stream.forEach(System.out::println);
}
}

Output:

1
2
3
4
5

Real-World Use Case

Creating a Stream of Configurations

In real-world applications, the Stream.of() method can be used to create a stream of configuration settings or parameters for processing.

Example

import java.util.stream.Stream;

public class StreamOfConfigExample {
static class Config {
String key;
String value;

Config(String key, String value) {
this.key = key;
this.value = value;
}

@Override
public String toString() {
return key + "=" + value;
}
}

public static void main(String[] args) {
Config[] configs = {
new Config("url", "http://example.com"),
new Config("timeout", "5000"),
new Config("maxConnections", "100")
};

// Create a stream from an array of configurations
Stream<Config> configStream = Stream.of(configs);

// Print the configuration settings
configStream.forEach(System.out::println);
}
}

Output:

url=http://example.com
timeout=5000
maxConnections=100

Conclusion

The Stream.of() method is used to create a sequential Stream from a given set of values or from an array. This method is particularly useful for quickly generating streams for processing.

By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring flexibility and ease of use when working with known sets of elements.

Original Post:

Java Stream of() Method

--

--

No responses yet