T
T
Tidus2022-02-05 15:19:42
Java
Tidus, 2022-02-05 15:19:42

Difference in streams of collections and array?

Explain why arrays have a stream implemented through the arrays class, while collections have it as their own method?
And why are streaming methods different? Collections do not have, for example, such thermal methods as sum, max, min (you can get them differently, of course, but still). In general, why is it implemented differently?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2022-02-05
@Akela_wolf

Because in Java, arrays and collections are completely different things (although similar in purpose). The array is located as close as possible to the sish array (except that the out-of-bounds check is built-in in Java, but otherwise it is almost the same). Accordingly, an array cannot have its own methods, because is not an object (similar to primitive data types, essentially an array is a primitive data type in Java). A collection, on the other hand, is an object. Which is what makes the difference between them. This difference could be smoothed out at the language level, but at the time of Java development there was no understanding of how to do it more conveniently. And then - the burden of backward compatibility had to be borne through the years (a similar story happened with type erasure). And this is such a fundamental difference that exists at the JVM level, so even modern languages,

B
BorLaze, 2022-02-05
@BorLaze

And what is the difference between collections and arrays, can you tell?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question