question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add size method for immutable collection builders

See original GitHub issue

It’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:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
electrumcommented, Aug 31, 2017

@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 for ImmutableList, ImmutableMap, and any others where it makes sense and is free. Additionally, we could add an isEmpty() 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 for ImmutableList).

0reactions
cpovirkcommented, Aug 31, 2017

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 because build() is free in that case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found