C# Linq vs Java Streams

At some time in the past, when I was learning linq to objects, I’ve really enjoyed article by Jon Skeet
about reimplementing linq to objects. The beauty of .net 3.5 changes was all about features added to language which allows to anyone build such stuff as linq. What does it mean at all? It simply meant if you wanted to write own linq you were able to do that quite simply, thanks to features added to language (like extension methods, yield return – which already was there, anonymous types, lambda expressions, etc). All in all, following suggestions of Jon Skeet plus my knowledge, I was finally able to build linq from scratch which allowed me to understand it very deeply.

On the other side, when you look on Java implementation of streams it looks … very dirty. Firstly it’s not that easy to understand the concept used to implement it, as there is many happenings in the implementation itself as opposed to usage of language features. And going forward, there are good things like default methods on interfaces level (kind of extension methods variation) which allowed to extend existing interfaces and classes with new stream API. Lambda expressions allowed to go step further to, to simplify look & feel of stream API.

But the biggest case for myself is lack of yield return like functionality, which introduced need for using spliterators (nice name). Going further, if you want to implement by yourself ArrayList functionality of stream API, it becomes not that easy. Lots of internal classes (which is of course good, because hiding implementation is never bad idea), StreamSupport interns makes it not easy to implement it from scratch, which just give you some layer of API you have to use making it not that fun (and easy to understand).

Going further, the lack of some language features doesn’t mean you cannot do that in java, so as in stack overflow discussion it’s doable to have yield return like functionality in java, but it’s much more verbose. Adding other parts of it to the game makes the final implementation quite complicated. Hopefully, as most of it is well hidden it gives a chance that as some point it will be simplified with new features of language simplifying life of developer.

And the Java language feature I’m missing the most now is this concept of simple iterators / generators / yield return
functionality (iterators and generators as JavaScript language feature). And once again, if you really, really miss such features and want to stay in JVM world, Scala again supports you with having anything you need to be efficient developer creating nice and clean code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s