LongStream class got introduced as part of Java 8 Numeric Streams addition, and it is located inside the java.util.stream package. LongStream class represents a sequence of primitive long-valued elements. In other words, it’s a long primitive specialization of a Stream.
Working with a LongStream is very similar to working with an IntStream class. Methods and operations are almost the same.
How to create a LongStream in Java?
There are several ways of creating a LongStream:
1. LongStream.of() method
Using the of() method, we can specify the values we want the LongStream to contain. There are 2 types of the of() method, one which takes only one element and the one that can take more than one.
Syntax:
LongStream.of(7); LongStream.of(7, 8, 9);
Here, the first statement will create a LongStream that will hold only one element (7). The second statement creates a LongStream that holds values 7, 8, 9.
2. LongStream.range() and LongStream.rangeClosed() methods
With range() and rangeClosed() methods, we define a range of long values that we want to create a stream of. The rangeClosed() method includes the ending element, while range() excludes it.
Syntax:
LongStream.range(1, 3); LongStream.rangeClosed(1, 3);
Here, the range() function will return a LongStream of values 1 and 2. The rangeClosed() function returns a LongStreamof 1, 2 and 3.
3. LongStream.iterate() method
We use the iterate() method to create infinite streams. However, we can limit it by using the limit() function.
Syntax:
LongStream.iterate(0, i -> i + 2).limit(10);
Here, we created a LongStream that contains values: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18].
4. LongStream.generate() method
Returns an infinite sequential unordered stream where the provided LongSupplier generates each element. This is suitable for generating constant streams, streams of random elements, etc.
Syntax:
LongStream.generate(() -> (long) (Math.random() * 5)).limit(10);
The above code will return a LongStream of 10 random numbers multiplied by 5.
Examples
Example 1:
Iterate over a LongStream.
LongStream supports the forEach() method for iterating over the elements, just like the regular Stream.
class Test { public static void main(String[] args) { LongStream longStream = LongStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); longStream.forEach(item -> System.out.print(item + " ")); } }
Output: 1 2 3 4 5 6 7 8 9 10
Example 2:
Convert a LongStream to a List.
Since Collections in Java can not store the primitive values directly, we need to use the boxed() method of LongStream class to convert the primitive type to the Wrapper class.
class Test { public static void main(String[] args) { List<Long> numbers = LongStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .boxed() .collect(Collectors.toList()); System.out.println(numbers); } }
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 3:
Convert a LongStream to an array using the toArray() function:
class Test { public static void main(String[] args) { long[] array = LongStream.of(1, 2, 3, 4, 5).toArray(); Arrays.stream(array).forEach(item -> System.out.print(item + " ")); } }
Output: 1 2 3 4 5
Example 4:
Filter a LongStream using the filter() function:
class Test { public static void main(String[] args) { LongStream.range(1, 10) .filter(i -> i % 2 == 0) .forEach(item -> System.out.print(item + " ")); } }
Output: 2 4 6 8
I hope this tutorial was helpful to you. To learn more, check out other Java Functional Programming tutorials.