2

I recently started using Java 8's Stream API and I think it's a great contribution to Java. However, I don't understand why it was implemented the way it was.

One of the other, most wonderful features in Java 8 is default-methods in interfaces, which allow having a default implementation to an interface. Using these, the Collection interface could've been endowed with all the methods offered through the stream API together with default implementations, all while being backwards-compatible. It would've allowed simpler syntax which is more like LINQ in .NET, but also allowing implementing types to override behavior of these methods. It would eliminate the need for calling the stream() method each time, and the need for using the collect() method at all times.

I know this is a very general question but to me it just seems like the developers of Java 8 made a very unfortunate choice which will be very hard to fix.

Dean Gurvitz
  • 854
  • 1
  • 10
  • 24
  • 1
    You seem to assume that a `Stream` should only be used with or only comes from a `Collection`. – Sotirios Delimanolis Aug 11 '15 at 16:39
  • 2
    [Here's](http://stackoverflow.com/questions/28459498/why-are-java-streams-once-off) an interesting post. – Sotirios Delimanolis Aug 11 '15 at 16:39
  • 2
    see also http://stackoverflow.com/questions/31188231 – ZhongYu Aug 11 '15 at 16:55
  • 2
    It's a legit question why don't we add `filter` `map` etc as default methods on collection interfaces. The main obstacle I think is because these interfaces have existed for too long and there are too many subclasses in the wild. adding default methods will very likely break a lot of subclasses. – ZhongYu Aug 11 '15 at 16:57
  • 2
    The main reason was to avoid confusion between mutability and immutability. Obviously the legacy Collections library is mutable and Streams encourage immutability (even if the streams are mutable). This distinction could cause confusion and so we have to explicitly convert to and from the Stream class. A better idea is to use immutable classes from a Java FP library. – Mark Perry Aug 12 '15 at 01:59

0 Answers0