Generate Stream from an array of primitive types in Java

Sometimes we use arrays of primitive types such as char and boolean. It is often possible to operate concisely by using the Stream API for arrays. However, ʻArrays # stream cannot be applied to primitive arrays other than ʻint, long, double, so make a note of what to do in such cases.

Use wrapper classes from the beginning

ʻArrays # stream` can be used for arrays of any object type, so how to use the wrapper class.

final Character[] array = {...};

Generate and map indexes with ʻIntStream`

favorite ʻIntStream # range` is used to generate indexes for the target array and map them to the elements of the array.

final char[] array = {...};
IntStream.range(0, array.length) //Note that it is not rangeClosed
         .mapToObj(i -> array[i])


Stream # flatMap is convenient when you want to operate the double array by stream at once. Example: I want to check if all the elements of a boolean type double array are true.

public void run() {
  final boolean[][] table = ...;

private Stream<Boolean> flatten(final boolean[] array) {
  return IntStream.range(0, array.length)
                  .mapToObj(i -> array[i]);

Recommended Posts

Generate Stream from an array of primitive types in Java
List of types added in Java 9
Get a non-empty collection from an Optional stream in java
Cast an array of Strings to a List of Integers in Java
Map without using an array in java
Compare the elements of an array (Java)
From Java9, the constructor of the class corresponding to primitive types is deprecated.
[Java] Get a random value from an array
Generate OffsetDateTime from Clock and LocalDateTime in Java
Equivalence comparison of Java wrapper classes and primitive types
About an instance of java
A quick explanation of the five types of static in Java
[Java11] Stream Summary -Advantages of Stream-
A program (Java) that outputs the sum of odd and even numbers in an array
I investigated Java primitive types
Java Stream API in 5 minutes
Easily get an integer from a system property in Java
Get attributes and values from an XML file in Java
Use Redis Stream in Java
[Java8] Sort int type array in descending order using stream
Implementation of tri-tree in Java
Note No. 1 "Counting and displaying duplicate values in an array" [Java]
[Java] Generate Data URI from byte string of file contents [Kotlin]
Concatenate strings returned by methods of multiple objects in Java Stream
Want to throw an IOException out of Stream? in that case
How to get the length of an audio file in java
Effective Java Item 25 Select a list from an array First half
Let's summarize the Java 8 grammar from the perspective of an iOS engineer
I sent an email in Java
Java learning memo (creating an array)
About Lambda, Stream, LocalDate of Java8
Java primitive types, reference types, Immutable, Mutable
Generate CloudStack API URL in Java
Java array variables are reference types
Try an If expression in Java
List of members added in Java 9
I made an annotation in Java.
Basic processing flow of java Stream
Java array / list / stream mutual conversion list
Implementation of like function in Java
Do you use Stream in Java?
Run an external process in Java
Types of exceptions in business systems
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
[Swift] Summary of how to remove elements from an array (personal note)
The story of acquiring Java Silver in two months from completely inexperienced.
How to convert an array of Strings to an array of objects with the Stream API
I want to ForEach an array with a Lambda expression in Java
[Java] Generate a narrowed list from multiple lists using the Stream API
Generate source code from JAR file with JD-GUI of Java Decompiler project
When calling println etc. from an external Java class file in Processing
Study Deep Learning from scratch in Java.
[Java] I participated in ABC-188 of Atcorder.
Implementation of DBlayer in Java (RDB, MySQL)
When seeking multiple in a Java array
Get the result of POST in Java
Significance of interface learned from Java Collection
Data processing using stream API from Java 8
Call Java method from JavaScript executed in Java
Try using the Stream API in Java
Reverse Key from Value in Java Map