Java LongStream of() Method

The of() method in Java, part of the java.util.stream.LongStream interface, is used to create a sequential LongStream from a specified array of long values or individual long values. This method is useful when you need to create a stream from a fixed set of long values.

Table of Contents

  1. Introduction
  2. of() Method Syntax
  3. Understanding of()
  4. Examples
    • Basic Usage
    • Creating a Stream from an Array
  5. Real-World Use Case
  6. Conclusion

Introduction

The of() method is a static method that allows you to create a LongStream from specified long values. This method can be used to create streams from a fixed set of long values, either by passing the values directly or by passing an array of values.

of() Method Syntax

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

Creating a Stream from Individual Values

static LongStream of(long... values)

Creating a Stream from an Array

static LongStream of(long t)

Parameters:

  • values: The long values to include in the stream.
  • t: A single long value to include in the stream.

Returns:

  • A new LongStream containing the specified values.

Throws:

  • This method does not throw any exceptions.

Understanding of()

The of() method allows you to create a LongStream from specified long values or an array of long values. This is useful for quickly creating a stream when you already know the values you want to include.

Examples

Basic Usage

To demonstrate the basic usage of of(), we will create a LongStream from individual long values.

Example

import java.util.stream.LongStream;

public class OfExample {
    public static void main(String[] args) {
        // Use of() to create a LongStream from individual long values
        LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);

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

Output:

1
2
3
4
5

Creating a Stream from an Array

This example shows how to use of() to create a LongStream from an array of long values.

Example

import java.util.stream.LongStream;

public class OfArrayExample {
    public static void main(String[] args) {
        long[] values = {10L, 20L, 30L, 40L, 50L};

        // Use of() to create a LongStream from an array of long values
        LongStream stream = LongStream.of(values);

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

Output:

10
20
30
40
50

Real-World Use Case

Creating a Stream of Transaction Amounts

In real-world applications, the of() method can be used to create a stream of transaction amounts from a fixed set of values.

Example

import java.util.stream.LongStream;

public class OfTransactionsExample {
    public static void main(String[] args) {
        // Create a stream of transaction amounts using of()
        LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);

        // Print the transaction amounts
        transactionAmounts.forEach(amount -> System.out.println("Transaction Amount: " + amount));
    }
}

Output:

Transaction Amount: 1000
Transaction Amount: 2000
Transaction Amount: 1500
Transaction Amount: 3000
Transaction Amount: 2500

Conclusion

The LongStream.of() method is used to create a sequential LongStream from a specified array of long values or individual long values. This method is particularly useful for quickly creating a stream from known values. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that you can easily create streams from fixed sets of long values.

Comments