Back to Stream Creation & Utilities
Q12Question 12 of 18

Create a stream of fixed values using Stream.of("A", "B", "C")

Key Concepts
  • Stream.of(values...) is a varargs factory that creates a finite, ordered, sequential stream
  • Stream.of("A", "B", "C") produces a three-element Stream<String> ready for map, filter, or collect
  • Single-argument Stream.of(x) makes a one-element stream
  • Stream.of(primitiveArray) creates a one-element stream of the whole array, not its elements
  • For arrays, use Arrays.stream(array) instead of Stream.of to stream individual elements
  • Best suited for small fixed sets in tests and ad-hoc data, not for large datasets

Definition

`Stream.of("A", "B", "C")` builds a small finite stream from the listed values, ready for any stream pipeline.
  • Stream.of(T... values) is a varargs factory producing an ordered, sequential, finite stream.
  • Three elements in this case: "A", "B", "C" as a Stream.
  • Single argument Stream.of(x) gives a one-element stream.
  • Primitive array gotcha: Stream.of(int[]) creates a one-element stream of the whole array.
  • Best use: small fixed sets, unit tests, and ad-hoc data in examples.

Priya at National Bank of India makes a short conveyor belt pre-loaded with exactly the items she names: A, then B, then C. The belt is ready to process, but she controls exactly what is on it.

Understanding the Concepts

What is Stream.of?

Stream.of(T... values) is a static varargs factory on the Stream interface. It creates a finite, ordered, sequential stream containing exactly the values you pass. The stream is ready to chain with map, filter, collect, or any other stream operation.

How do you use it?

JAVA
package com.bank.core;

import java.util.stream.Stream;
import java.util.List;

public class StreamOfDemo {

    public static void main(String[] args) {
        // Three fixed string values
        List<String> tierCodes = Stream.of("GOLD", "SILVER", "BRONZE")
            .map(String::toLowerCase)
            .toList();
        System.out.println("Tiers: " + tierCodes);

        // Single element
        Stream.of("SAV2001")
            .forEach(id -> System.out.println("Account: " + id));

        // Works with any reference type
        Stream.of(100, 200, 300)
            .mapToInt(Integer::intValue)
            .sum();  // 600
    }
}
TEXT
Tiers: [gold, silver, bronze]
Account: SAV2001

What is the primitive array gotcha?

Stream.of with a single reference array streams its elements. But with a single primitive array, it creates a one-element stream containing the whole array.

JAVA
String[] names = {"Rajesh", "Priya", "Meera"};
Stream.of(names).count();  // 3: streams the elements

int[] numbers = {10, 20, 30};
Stream.of(numbers).count();  // 1: one-element stream of the int[]

// CORRECT for primitive arrays:
java.util.Arrays.stream(numbers).count();  // 3: streams the elements
java.util.stream.IntStream.of(10, 20, 30).count();  // 3

How is Stream.of different from other creation methods?

| Factory | Use case | Finite? | Type |
|---|---|---|---|
| Stream.of(a, b, c) | Small fixed values | Yes | Stream |
| Arrays.stream(array) | Existing array | Yes | Stream or IntStream |
| collection.stream() | Existing collection | Yes | Stream |
| Stream.generate(supplier) | Infinite independent values | No (needs limit) | Stream |
| Stream.iterate(seed, f) | Infinite dependent sequence | No (needs limit) | Stream |

What are common mistakes?

Mistake: Using Stream.of with a primitive array expecting element-by-element streaming.

JAVA
// WRONG: creates Stream<int[]> with one element
int[] salaries = {50000, 60000, 70000};
long count = Stream.of(salaries).count();  // 1, not 3

// CORRECT: use Arrays.stream for primitive arrays
long count = java.util.Arrays.stream(salaries).count();  // 3

When should you use Stream.of vs other factories?

Use Stream.of for a handful of fixed literal values in tests or small constant sets. For data from an existing array, use Arrays.stream. For data from a collection, use collection.stream(). For large data, always start from the source's own stream method because it provides better spliterator characteristics.

What about empty and nullable streams?

JAVA
// Empty stream
Stream.empty().count();  // 0

// Nullable: Stream.ofNullable (Java 9+)
String value = null;
Stream.ofNullable(value).count();  // 0 (empty stream for null)

String present = "SAV2001";
Stream.ofNullable(present).count();  // 1

What should a senior engineer know?

Stream.of is convenient for tests and small constants but should not be used for large data. For production code with real data, use the source's own stream() method. A senior engineer picks the right factory for the source: Stream.of for literals, Arrays.stream for arrays, Collection.stream() for collections, and primitive stream factories for primitives.

What are common interview follow-up questions?

  • "What happens with Stream.of(primitiveArray)?" Creates a one-element stream of the whole array.
  • "How do you create an empty stream?" Stream.empty().
  • "What is Stream.ofNullable?" Java 9 method that returns an empty stream for null or a single-element stream for a non-null value.

Visual Overview

DIAGRAM
  Stream.of("GOLD", "SILVER", "BRONZE")
       │
       │  ordered, sequential, finite
       ▼
  ┌──────────────────────────────┐
  │  "GOLD" → "SILVER" → "BRONZE"│
  └──────────────────────────────┘
       │
       │  .map(String::toLowerCase)
       ▼
  ┌──────────────────────────────┐
  │  "gold" → "silver" → "bronze"│
  └──────────────────────────────┘
       │
       │  .toList()
       ▼
  ["gold", "silver", "bronze"]

Key Takeaway

Stream.of is a varargs factory that builds a finite, ordered, sequential stream from the values you list. Stream.of("A", "B", "C") gives a three-element Stream that you can map, filter, or collect like any other stream. At the National Bank of India, Rajesh uses it in unit tests and for small fixed sets like account tier codes.

The classic gotcha is the primitive array behavior. Stream.of with a reference array streams its elements, but with a primitive array it creates a one-element stream containing the whole array. For primitive arrays, use Arrays.stream or IntStream.of instead.

The senior rule is to pick the right factory for the source: Stream.of for a few literals, Arrays.stream for an existing array, Collection.stream() for a collection, and primitive stream factories for primitives. Stream.of is perfect for tests and small constants but not for large data, where the source's own stream method provides better spliterator characteristics.

Remember one sentence: Stream.of(values...) is the quick factory for small fixed sets, but use Arrays.stream for arrays and collection.stream() for collections.

---

Share
Use ← → arrow keys to navigate