Java Stream.of() and IntStream.range()

Table of Contents

In Java, the Stream API provides powerful capabilities for working with collections and performing various operations on the elements. Two commonly used methods in the Stream API are Stream.of() and IntStream.range(). In this article, we’ll explore how these methods can be used to create streams in Java and leverage their functionalities.

Stream.of()

The Stream.of() method is used to create a stream from a sequence of elements. It accepts a variable number of arguments and returns a stream containing those elements. Let’s see some examples:

Example 1: Stream.of() with String elements

import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("apple", "banana", "orange");
        stream.forEach(System.out::println);
    }
}

In this example, we create a stream using Stream.of() and pass three strings as arguments. We then iterate over the stream using the forEach() method and print each element to the console. The output will be:

apple
banana
orange

Example 2: Stream.of() with Integer elements

import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
        stream.forEach(System.out::println);
    }
}

In this example, we create a stream using Stream.of() and pass five integers as arguments. We iterate over the stream and print each element to the console. The output will be:

1
2
3
4
5

The Stream.of() method is useful when we want to create a stream directly from a set of elements without using a collection.

IntStream.range()

The IntStream.range() method is specifically used to create a stream of integer values within a specified range. It takes two arguments: the starting value (inclusive) and the ending value (exclusive). Let’s see some examples:

Example 1: IntStream.range() with a simple range

import java.util.stream.IntStream;

public class IntStreamRangeExample {
    public static void main(String[] args) {
        IntStream range = IntStream.range(1, 5);
        range.forEach(System.out::println);
    }
}

In this example, we create an IntStream using IntStream.range() with the range from 1 to 5 (exclusive). We then iterate over the stream and print each element to the console. The output will be:

1
2
3
4

Example 2: IntStream.range() with a step value

import java.util.stream.IntStream;

public class IntStreamRangeExample {
    public static void main(String[] args) {
        IntStream range = IntStream.range(1, 10).filter(n -> n % 2 == 0);
        range.forEach(System.out::println);
    }
}

In this example, we create an IntStream using IntStream.range() with the range from 1 to 10 (exclusive). We then apply a filter to only include even numbers. Finally, we iterate over the stream and print each element to the console. The output will be:

2
4
6
8

The IntStream.range() method is commonly used in scenarios where we need to perform operations on a sequential range of integers.

Conclusion

The Stream.of() and IntStream.range() methods are valuable additions to the Stream API in Java. They provide convenient ways to create streams from individual elements or within a specific range of integers, respectively. By leveraging these methods, we can harness the power of the Stream API and perform various operations on the elements efficiently.

In this article, we explored the usage of Stream.of() and IntStream.range() methods, demonstrated how to create streams using these methods, and showcased examples of iterating over the streams. By incorporating these methods into our Java code, we can streamline our development process and write more concise and expressive code.

We started with the Stream.of() method, which allows us to create a stream directly from a sequence of elements. We showed examples of creating streams with both String and Integer elements, highlighting how easy it is to work with different types of data.

Next, we delved into the IntStream.range() method, which enables us to generate a stream of integers within a specified range. We demonstrated how to create simple ranges as well as ranges with step values, emphasizing the flexibility and versatility of this method.

With the Stream API, we can perform a wide range of operations on streams, such as filtering, mapping, reducing, and more. These operations can be combined to create complex data processing pipelines, making our code more readable and maintainable.

In conclusion, the Stream.of() and IntStream.range() methods are powerful tools in the Java Stream API. They provide convenient ways to create streams and work with collections of elements. By mastering these methods and understanding their capabilities, we can unlock the full potential of the Stream API and write efficient and expressive code in our Java applications.

Happy streaming with Java!

Command PATH Security in Go

Command PATH Security in Go

In the realm of software development, security is paramount. Whether you’re building a small utility or a large-scale application, ensuring that your code is robust

Read More »
Undefined vs Null in JavaScript

Undefined vs Null in JavaScript

JavaScript, as a dynamically-typed language, provides two distinct primitive values to represent the absence of a meaningful value: undefined and null. Although they might seem

Read More »