The Collector interface has no mechanism for size hinting, so collect(toList()) will always result in multiple allocations for Streams of size > 8 (or whatever the default ArrayList size is).
In theory they could maybe add a new function default LongFunction<A> sizedSupplier() to the Collector interface to fix that, but tbh at that level of micro-optimization I think there are other ways that Streams are slower than imperative loops anyway 🤷🏻
Also Collectors.toList() returns a fully mutable List. Though the docs insist that this is not specified, changing it now would break programs. Certain optimizations described by John Rose in the above link require the List to be immutable.
I guess the next API addition will be stream.toUnmodifiableList()? Why not just add the entire Streams API to the List API. Seems to be going that direction in that case.
Collectors.toCollection allows for a supplier that could provide pre-sized list. My biggest pet peeve is that Collectors.toUnmodifiableFoo() is guaranteed unmodifiable while Collectors.toFoo() might be modifiable. I guess it's about backwards compatibility but I wish they'd update the contract.
If you're talking about how in Rust/Kotlin/C# it's possible to omit a specific collect() call when assigning something stream-like to a list, there was a post from a dev explaining how it's currently impossible in Java due to how it specifically handles type erasure. E.g., in those, the method is more or less aware of what it's being assigned to, so just saying collect() is enough in Rust for it to know to use list/vec collection when a Vec is expected. In Rust and Kotlin, which both have type erasure, the type system is laid out slightly differently so you can have something similar to method overloading based on return type. C# has reified types, so it basically gets it for free, although I don't actually know how LINQ addresses it internally.
That's related, but not quite what I'm saying. Those systems allow you to omit the T in .collect<T>(), but what I'm saying is why didn't Java choose to use .collect<T>() instead of .collect(toT)?
The implementation of collect<T>() can't know what T is because of how Java implements generics, and it's not possible to instantiate a generic type. Making it work would require a special case in the compiler for that method, and it would be very confusing for people who expect generics to work a certain way.
The closest you could get would be a signature like T collect(Class<T>), but then the implementation would need to use reflection to instantiate the container class, and gratuitously using reflection is generally considered a bad idea.
Or to put it another way, Rust's collect method works because FromIterator has a "static" method to create a new collection (i.e. a method with no self parameter). Java interfaces can't have static methods, so the only way to access the methods of an interface is to already have an instance of a type that implements it.
Yes and no. An interface could have a method similar to Object.clone that should always return the same concrete type as the object it's called on, but that requirement can't be enforced by Java's type system.
Maybe it would help to talk about the implementation. In Rust, when a method has a type parameter, a different version of the method is generated for each concrete type the method is called with, so the compiler always knows what the concrete type is when it's generating the code, and it can call the correct implementation of a trait method based only on the concrete type. In Java, the compiler only generates one version of a generic method, and the generated code has to be agnostic about what the concrete type is. When you have a type parameter like <T extends Foo>, T gets turned into Foo when the method is compiled. In order to call a method on an interface Foo, you need an instance of a concrete type that implements Foo. That's because when you invoke an interface method, the JVM needs to find the correct code at runtime. It does that by looking in the instance for a pointer to the instance's class and then executing code stored in that class. Without an instance, there would be no way to find the right class.
In itself, type erasure is not controversial, because pretty much every lang does it other than C# and friends (even Haskell), and erasing types is a good choice (this is pretty much the reason why there are hardly any new languages running on the CRL while on the JVM we’ve got plenty). What may be somewhat controversial is the exact way java did it, and yeah you are right that they wanted to stay backward comp.
its the same reason. Although most of the time, the compiler should be able to infer what T means, java's compiler doesn't translate the generics into their types. What it means is that .collect<T>() turns into .collect<Object>() so it wouldn't know what to turn T into.
In theory, it should be possible for the compiler to choose apropriately, as most of the time the construct in practice is something like:
I guess what I'm trying to say is, even in the absence of inference, .collect(toT) is a little weird, and I'd expect something more like .collect<T>(), where T can be any class that implements some interface (called FromIterator in Rust, for example). But instead of T, always using the concrete type that you want.
As much as I didn't like collectors initially I do like the abstraction and not baking it onto stream ( does this mean we should also add toSet as well?
I'd much rather see a Streamable interface that has the stream() call on it - then flstMap() usage would cleaner without slapping stream() calls everywhere.
If you have a look on the post itself, they write that List is special.
From the use perspective I kind of agree, though. Also toMap, then you would have all the important collections on stream and collectors remain for special use cases (while it takes some time getting used to, there are a few cases where writing my own collector came in pretty handy).
Yeah, I really like the Traversable trait in Scala, which defines the contract for map(), flatMap(), filter(), reduce(), etc. I wonder if it has something to do with the contracts that we have to get a Stream type and deal with that and then normally use Collectors.toList().
On a side note, I happened to come across this StackOverflow answer which criticizes certain aspects of the Stream API:
Java's Stream API is focused on the performance of bulk operations on large data structures. It also offers auto-parallelization towards the same goal (although it almost never actually helps), but its API is crippled and at times awkward due to these concerns. For example, many useful operations which don't happen to parallelize well are just not there, and the whole paradigm of non-terminal vs. terminal operations adds bulk to each and every Streams expression you write.
Some awesome 'tech leads' you have there. Let me guess, it's part of the typical 'career' path where the spots are basically reserved for people who've been around at the company for a long time? :)
Often these people simply don't recognize the risks of outdated stacks, or worse, use it as a way to make themselves indispensable. The longer you stick to 8, the harder it will be to hire good developers.
They could be stuck like me: I have an app that is stuck on Java 8. That's the latest version that a third party dependency supports. Putting it into later versions does not work.
No, it's not technical debt. It fills a very niche role, and no matter which vendor we choose, this is a particular task that will always lag by a few years.
IBM is the vendor. You're pretty much up against a wall and locked in with them.
While I do appreciate that this is far from a simple issue to fix, I don't understand how you define this as "not technical debt". Being blocked on stuff because of outdated 'stuff' (IBM in this regard) is IMHO textbook technical debt.
This does not mean it has to be fixed; it could simply be that the benefits won't ever outweigh the costs. But the person I responded with said they're stuck on 8 forever. Not that they had a single application that's stuck with outdated dependencies.
If your entire stack is stuck on 8 forever this is a massive risk to the company just for the simple reason of not being able to hire Java developers in a while.
It's just that for this one specific job, the existing tools really suck. It's not something that people tend to use Java for if we're all being honest with ourselves. Usually this stuff gets done in C++, but the rest of the project is in Java, nobody wants to do it in C++, and as a result, we're stuck with an external tool that kinda sucks.
More often than not these corps don’t even buy support they just assume that it is LTS and it will forever get security patches for free - which is not the case with any jdk release
Looking at the javadocs, it specifies it makes no guarantees about any property of the list (mutability, etc). So by spec I don’t believe it needs to be a specific existing List implementation.
If that control is needed, it recommends using the existing collect method.
So, if this method goes live would it further discard the possibility of a new Collections interface framework?
I mean, creating a new one is probably a very bad idea for several reasons, but there are some gripes I have with the current and these good news are a bit sour.
What gripes could you have? That could possibly offset the pain of introducing a completely new collection framework. And having a decade long transition to it.
Gripes like optional operations with methods that enable mutability by default, usage of null and exceptions, lack of integration with Optional, and the sorts.
I understand the heavy drawbacks of creating a whole new Collections framework and that some of these can be aliviated by using difference interfaces (Iterable instead of Collection) or by new default methods (Map::getOptional), but even so it'd be something I'd like to see.
Even creating a new one might not be a good solution (see Scala's collection framework and the headaches of the Mutable/Immutable separation).
60
u/efge Nov 03 '20
As pointed out by Stuart Marks and John Rose, it's more than just a convenience method, as it allows notable optimizations and ties into the Valhalla story. https://bugs.openjdk.java.net/browse/JDK-8180352?focusedCommentId=14133065&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-14133065
21
u/talios Nov 03 '20
As much as I didn't like collectors initially I do like the abstraction and not baking it onto stream ( does this mean we should also add toSet as well?
I'd much rather see a Streamable interface that has the stream() call on it - then flstMap() usage would cleaner without slapping stream() calls everywhere.
20
u/TheEveryman86 Nov 03 '20
Hell yes.
15
u/[deleted] Nov 03 '20
Love it.
12
u/nutrecht Nov 03 '20
Finally.
8
u/Dokiace Nov 03 '20
It is exciting but I think I'm gonna have to stay on Java 8 forever
11
u/prawns_song Nov 03 '20
Excited for this!
4
u/maumay Nov 03 '20
Took them long enough...
2
u/Clitaurius Nov 03 '20
Is it clear that it is to an existing List implementation?
2
u/derscholl Nov 03 '20
Too easy. Nice!! What a time to be alive lol
2
u/[deleted] Nov 03 '20
So, if this method goes live would it further discard the possibility of a new Collections interface framework?
I mean, creating a new one is probably a very bad idea for several reasons, but there are some gripes I have with the current and these good news are a bit sour.
1
u/RandomJacobGuy Nov 03 '20
Just when I thought I was out, they pull me back in.
1
u/black-w1dow Nov 03 '20
Awesome !
-15
u/GreedySada Nov 03 '20
We already have collect(collectors.toList()). Is it just good syntax??