Add size method for immutable collection builders
See original GitHub issueIt’s often necessary to know the size of the collection being built. For example, to special case if this is the first element, or if the collection is empty. The workarounds are ugly.
This should be free for most builders since they already track the size. I only care about this for List
, Set
, and Map
, so if it’s fine if it can’t be added for more exotic collections like ListMultimap
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
3 Creating Immutable Lists, Sets, and Maps - Oracle Help Center
Convenience static factory methods on the List, Set, and Map interfaces, which were added in JDK 9, let you easily create immutable lists,...
Read more >com.google.common.collect.ImmutableSet.size java ... - Tabnine
A factory that chooses the most space-efficient representation of the table. */ static <R, C, V> RegularImmutableTable<R, C, V> forOrderedComponents( ...
Read more >ImmutableList.Builder (Guava: Google Core Libraries for Java ...
A builder for creating immutable list instances, especially public static final lists ("constant lists"). Example: public static final ImmutableList<Color> ...
Read more >Elegant way to define a huge global ImmutableList constant ...
Builder <Person> builder = new ImmutableList. ... Also, the compiled code has a size close to the method size limit, so there's not...
Read more >ImmutableList<T>.Builder Class (System.Collections.Immutable)
Although ImmutableList<T>.AddRange and other methods already provide fast bulk change operations on the list, the ImmutableList<T>.Builder class allows ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jbduncan yes, exactly. Apologies for not explaining more clearly.
@lowasser that’s a great point about duplicates for
ImmutableSet
.We could add a
size()
method forImmutableList
,ImmutableMap
, and any others where it makes sense and is free. Additionally, we could add anisEmpty()
method for most/all builders. Knowing if the collection is empty will cover a good number of use cases, though sometimes you do need the size (most often forImmutableList
).In the cases where people have asked for this internally, we’ve found that their code reads better if it separates the “add things to builder” and “read from resulting collection” steps, rather than intermixing the two, even in the small way you suggest here. I would imagine that there are exceptions, of course, but overall, I fear that people will use this to make code worse rather than better.
We’ve also found that people try to avoid calling
build()
on an empty builder. That turns out not to be necessary becausebuild()
is free in that case.