[Java] The difference between the Stream.of () and Arrays.stream () methods that you do not know unexpectedly

Introduction

If you want to get a Stream from an array in Java, you can get it by using the Stream.of () method or the ʻArrays.stream ()` method.

At first glance, both methods seemed to give the same result, and I didn't know the difference between these methods.

So, when I examined the difference between these two methods, I found that there were two major differences.

1. Difference in returned type

When an array of primitive types (eg int []) is used as an argument to these methods, the returned types will be different.

For example, when int [] is used as an argument, the Stream.of () method returns Stream, but the ʻArrays.stream () method returns ʻIntStream.

Also, the ideal class for processing using a primitive type Stream is a primitive Stream such as ʻIntStream, so if you use the Stream.of () `method, you need to convert it to a primitive Stream.

It can be converted by using methods such as Stream.flatMapToInt () and Stream.flatMapToLong ().

2. Whether the argument is generics

When trying to get a Stream from an array of primitive types, the ʻArrays.stream ()` method only supports three primitive types: int [], long [], double [].

So, for example, char [] cannot be used as an argument to the ʻArrays.stream ()` method.

However, the reference type can be used as an argument without any problem.

On the other hand, the Stream.of () method has a generic argument, so any type of array can be used as an argument.

The above contents are summarized in Table 1 below.

Table 1. Relationship between each method and the types of arrays that can be used as arguments

Array type used for arguments Arrays.stream() Stream.of()
double type
float type ×
long type
int type
short type ×
byte type ×
char type ×
boolean type ×
String type
Class type

in conclusion

From the above, there are differences between the Stream.of () method and the ʻArrays.stream ()` method in the return type and the types that can be applied to the arguments, so be especially careful when using an array of primitive types. I found it necessary.

As an individual's impression, I think it's safer to use the Stream.of () method. We would appreciate it if you could let us know what you think about the proper use of these methods.

Thank you for reading to the end.

reference

https://www.geeksforgeeks.org/difference-between-stream-of-and-arrays-stream-method-in-java/ https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Arrays.html https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/stream/Stream.html

Recommended Posts

[Java] The difference between the Stream.of () and Arrays.stream () methods that you do not know unexpectedly
[Java] The difference between the Stream.of () and Arrays.stream () methods that you do not know unexpectedly
Java reserved word summary that you do not know unexpectedly
Convenient use of Map that you do not know unexpectedly
[Java] Understand the difference between List and Set
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
[Java] Difference between == and equals
Difference between Java and JavaScript (how to find the average)
[Java] Check the difference between orElse and orElseGet with IntStream
Think about the differences between functions and methods (in Java)
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
[Java] What is the difference between form, entity and dto? [Bean]
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Interpret the relationship between Java methods and arguments into a biochemical formula
Understand the difference between int and Integer and BigInteger in java and float and double
Difference between final and Immutable in Java
[For beginners] Difference between Java and Kotlin
About the difference between irb and pry
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Java] Difference between equals and == in a character string that is a reference type
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Difference between next () and nextLine () in Java Scanner
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Summarize the differences between C # and Java writing
[Java] Difference between "final variable" and "immutable object"
Error logging and exception handling that you can't often see in the Java area
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
[Ruby] Difference between get and post
Get the result of POST in Java
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
About the difference between classes and instances in Ruby
About the relationship between HTTP methods, actions and CRUD
[Java] Difference between static final and final in member variables
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
What is the difference between skip and pending? [RSpec]
[Rails] I investigated the difference between redirect_to and render.
[Swift] UITextField taught me the difference between nil and ""
Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==