jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.08k stars 167 forks source link

Add Collectable.toUnmodifiableList() and toUnmodifiableSet() for convenience #261

Closed pablogrisafi1975 closed 8 years ago

pablogrisafi1975 commented 8 years ago

In the good bad old days of java 1.7 + Guava I become used to ImmutableList. Since I move to java 1.8 + jool my lists and sets are all mutable again.

I can wrap my lists in Collection.unmodifiableList, writing code like

Collection.unmodifiableList(Seq.seq(list).filter(e -> e > 5).toList())

but I think it would be a nice addition to be able to write

Seq.seq(list).filter(e -> e > 5).toUnmodifiableList()

I think the list would in fact be immutable, since there is no way to access the underlying ArrayList, but I may be wrong...

Thanks in advance for your time reading this

Xaerxess commented 8 years ago

Right now you can do that in "fluent" way (note static imports from Collectors):

Seq.seq(list)
  .filter(e -> e > 5)
  .collect(collectingAndThen(toList(), Collections::unmodifiableList))

Hypothetical toUnmodifiableList() implemented in such way would be, as you point out, an immutable list, since jOOL won't mess with underlying list reference.

pablogrisafi1975 commented 8 years ago

Right now you can do that in "fluent" way

I didn't realize that. Thank you. However, it is even more typing!

Allow me to vote again for toUnmodifiableList, or even toImmutableList. A small improvement.

Xaerxess commented 8 years ago

Right now you can do that in "fluent" way I didn't realize that. Thank you. However, it is even more typing!

Welcome in Java! ;) Also, Guava 21 will be Java 8 compatible and will include immutable collections collectors, so you'll be able to write:

Seq.seq(list)
  .filter(e -> e > 5)
  .collect(ImmutableList.collector())

or something similar (I don't know whether the API is know right now.)

lukaseder commented 8 years ago

Thanks for the suggestion. Clearly, the Guava 21 collector is a good way forward, but I could definitely live with a toUnmodifiableList() (and toUnmodifiableSet()) convenience method. As you pointed out, there's no need to add an extra toImmutableList() as the unmodifiable list is already de facto immutable (it isn't 100% because it could be mutated via reflection)

pablogrisafi1975 commented 8 years ago

Thanks! I rather keep being guava free for a while!

lukaseder commented 8 years ago

@pablogrisafi1975 Excellent. Let me know if there's anything else Guava can do and that would fit in jOOλ easily!