r/programming • • Dec 11 '21

Teeing, a hidden gem in the Java API

https://blog.frankel.ch/teeing-java-api/
74 Upvotes

17 comments sorted by

13

u/hugogrant Dec 12 '21

Why is there so much jargon in the stream API?

It's also funny to see a unix reference in the jdk (tee is the unix command that does this for outputting on the shell).

1

u/przemo_li Dec 13 '21

Streams support Open/Closed Principle and existed for decades, therefore rich ecosystem of useful combinators is only expected.

Let's turn the table around. Why do you expect but a few operators? What design features of projects you worked on made such developments hard and thus uncommon?

1

u/SpaceToaster Dec 12 '21

Linq for C# is a lot cleaner in my opinion. Stream API never felt very natural or elegant to use

10

u/Sharlinator Dec 12 '21

The Java Stream API uses standard functional programming terms for the most part. LINQ uses standard RDB terms for the most part. It’s just what you’re accustomed to (I prefer the former FWIW).

3

u/vegetablestew Dec 12 '21

The Collectors API is particularly ugly. Wtf is a finisher?

5

u/Sharlinator Dec 12 '21 edited Dec 12 '21

I find the Collectors API actually surprisingly elegant. The complexity comes mostly from the fact that it supports parallel collection (as the Stream API is fundamentally designed to support work-stealing parallelism and map-reduce processing) so there needs to be a way to recursively compose partial results. But there are helper methods for creating collectors when you don’t need the full power of the API.

3

u/hugogrant Dec 12 '21

It's the last punch in a boxing match that ends in a KO or TKO, right?

1

u/przemo_li Dec 13 '21

Finisher is flashy move to do that data in ;)

12

u/ForeverAlot Dec 12 '21

Counterpoint: the LINQ API appropriates classic functional operations and attempts to restate them in terms of SQL, which is not a particularly clean language to emulate in the first place, with none of the runtime characteristics of SQL implementations. The result has zero transferability into or out of .NET.

(and I like SQL)

2

u/screwuapple Dec 12 '21

Query syntax is just one side of the LINQ api. The other side is method syntax (lambdas). Then factor in being able to dynamically create expressions and compile them to native delegates. LINQ is very powerful and full featured- I think you’re selling it short.

1

u/Pesthuf Dec 12 '21

The real strenght of LINQ is being able to use it on any kind of data source.
Try using the Java Stream API to generate SQL - you can't.
Because there's no way to get a lambda function as a parsed expression. You can only call them.

So many libraries that aim to "bring LINQ to language X" miss the point. It's more than a few convenient functions for working with arrays.

5

u/ForeverAlot Dec 12 '21

I never once missed this in Java. However, I have lost more time to friction caused by APIs that accept expressions instead of functions or that fail at runtime with incomplete support for operations than expressions have ever saved me, personally. If LINQ-to-SQL truly is the point of LINQ, I hope OpenJDK never tries to adapt it.

1

u/SpaceToaster Dec 13 '21

Run time errors are actually a very valid concern, as is finding compatible functions or operators occasionally when doing less common scenarios. Despite these issues, I’ve found it to have a massive benefit to readability and productivity when done right.

The parsed expressions do open the door for a lot of interesting use cases outside of linq, also. They feel at home in a language like C#, less so in Java.

12

u/Monsieur_Moneybags Dec 11 '21

Good to know this exists, though you need Java 12 or higher. I wish it were in Java 8, which is what I'm still forced to use at work.

11

u/[deleted] Dec 12 '21

If you're not yet on Java 12 then you can backport it with ~100 lines of code. Just go into Collectors and copy-paste bits until it compiles. It's only 2 methods, 1 class and 2 static fields.

I've done exactly that for other collectors. On my Java 8 codebase I have a class which is called something like FutureJDKCollectors. If/when we migrate to a newer JDK, I'll deprecate those and replace them with the real thing.

1

u/[deleted] Dec 12 '21

This is an interesting take, especially when it comes to using static imports, but does the license allows this?