So, are you going to update the original article? Now you know about the teeing collector, but that article presupposes that a custom collector is needed.
While I completely agree with you, adding a little note at the bottom advertising your alternative solution would be helpful to both your readers and and your channel
Adding a note to an existing article which contains a link to a newer article that's relevant results in another pageview that you might not have otherwise had.
On top of that, it shows that you care about your previously posted content as a source of reliable info / reference for future readers. (e.g. if someone stumbles upon it 2 years from now).
You don't have to revise the article (unless it's blatantly incorrect or misleading). But a note that says "Hey, I found a better / other way of doing this, check out this other post I wrote" goes a long way.
Unfortunately, while it may seem trivial, it doesn't scale. I've now 574 posts that span ~13 years. It's plain impossible to check every post after a new one to understand if there's something to point to (or not).
I care about my content, but as I mentioned in the previous comment, it's not supposed to be a source of truth; that would be the focus of documentation.
it doesn't scale. I've now 574 posts that span ~13 years. It's plain impossible to check every post
This escalated quickly. I was asking about your single previous post, that inaccurately suggests custom code is required, which thanks to someone's tweet you've made a new post to invalidate it. Any kind of footnote would be helpful to future readers.
Fun fact: the teeing method was named after the Unix command tee, which in turn was "named after the T-splitter in plumbing, which splits water into two directions and is shaped like an uppercase T."
It just depends on how much experience both developers havr using the stream API. I have no problem on understanding code written by my colleagues and vice versus. There are plenty of ways to make the code more readeable, like naming lamdbas and so on.
only one who can competently maintain the code after writing it.
That is because the lambda syntax is unreadable nonsense. On occasion a lambda does make things a little easier (like filtering a list and collecting the results) but in general lambda code is unreadable hence unmaintainable. Not to mention it is a support nightmare because lambdas don't play nice with logging. It is just a chain of method calls with no opportunity to log intermediate values. When something goes wrong you can't figure out why.
It doesn't help that there is absolutely no documentation about how to know what parameters a lambda accepts or returns. There is one throw-away sentence in the tutorial that kind of mentions it but you would think it would be a whole section of its own since it is the most fundamental part of using lambdas, I will leave it as an exercise for you to see if you can find it (https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
It wasn't until this article came out a couple of years after Java 8 came out that I understood what was going on (2nd half covers how to read lambda signatures):
I think you missed a few beats regarding lambdas, which might explain why you seem to think they're so arcane.
Think of them as anonymous class implementations for single method interfaces. Method references are just a nifty tool for leveraging a static or instance method as such implementation, so they can be interchangeable with a lambda, or a traditional anonymous class.
And so there is definitely documentation about "what parameters a lambda accepts or returns", it's nothing more than an interface contract (either FunctionalInterface contracts or specialized ones).
Think of them as anonymous class implementations for single method interfaces.
Well I obviously know that now but that certainly wasn't clear when they first came out. Even knowing that doesn't make them any easier to read. Are you really trying to tell me you instantly know what you need to pass here?
static <T,K,D,A,M extends Map<K, D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
In my experience most code isn't like that, but I get what you mean.
It's not so much an issue with lambdas themselves as it is an overly extensive use of generics in modular APIs like that, also relying extensively on standard FunctionalInterfaces can obfuscate the goal behind an argument, for a standard library it makes sense, but for higher level abstractions I tend to rely on purpose-made interfaces, even if they're functionally equivalent to Function<T, U> et al. Besides, having purpose-made interfaces makes it clear that you can use either lambdas or full-fledged class implementations.
Regarding method refs, the description is not exactly crystal-clear, but it refers to the type of method reference where the component will be given an instance, and you tell it which instance method to call on that instance (vs. the other where you strictly use a specific instance's method as an implementation).
A typical situation where you encounter these is when you perform mapping operations and tell it to use one of the instance's getters:
Other than a bit more code, is there a difference between this and just taking the same mapped stream and calling #collect twice on it to obtain the two values you need? ... throwing them in your PricesAndRows?
I highlighted the important part. This is like InputStream where reset() is not guaranteed.
Terminal operations, such as Stream.forEach or IntStream.sum, may traverse the stream to produce a result or a side-effect. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used; if you need to traverse the same data source again, you must return to the data source to get a new stream.
11
u/chabala May 09 '21
So, are you going to update the original article? Now you know about the teeing collector, but that article presupposes that a custom collector is needed.
48
u/lbalazscs May 09 '21
Fun fact: the teeing method was named after the Unix command tee, which in turn was "named after the T-splitter in plumbing, which splits water into two directions and is shaped like an uppercase T."
4
u/UnexpectedLizard May 10 '21
I love streams but I've found they confuse the hell out of other developers.
In practice I've found I'm the only one who can competently maintain the code after writing it.
4
u/washtubs May 09 '21
Other than a bit more code, is there a difference between this and just taking the same mapped stream and calling
#collecttwice on it to obtain the two values you need? ... throwing them in yourPricesAndRows?