Closed pablogrisafi1975 closed 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.
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.
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.)
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)
Thanks! I rather keep being guava free for a while!
@pablogrisafi1975 Excellent. Let me know if there's anything else Guava can do and that would fit in jOOλ easily!
In the
goodbad 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