Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have separate Spliterators class in Java 8?

Why did new Spliterators class appear in Java 8? Since Java 8 we have possibility to add static methods to the interfaces. Since Spliterators class has only static method wouldn't be simpler to declare all its methods in the Spliterator interface?

The same question about Collectors/Collector pair.

Thank you.

like image 763
Sergiy Avatar asked Dec 19 '22 10:12

Sergiy


1 Answers

It’s perfectly possible that this decision was made without even thinking about this brand new possibility, but simply following the established-since-twenty-years pattern.

Besides that, it can be debated whether it is really useful to add 25 to 30 static methods to an interface. It makes sense to offer a few factories for canonical implementations, but you should draw a line somewhere. It’s not feasible to add factories to all implementations to an interface, just because they are offered by the same library. But this debate would be off-topic.

Further, Spliterators does not only offer static methods, but also nested classes. Unlike static methods, these classes would pollute the name space of every implementation class, when being defined in an interface.

Collectors and Spliterators may also contain implementation-specific non-public methods and even fields.

like image 127
Holger Avatar answered Dec 29 '22 00:12

Holger