Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite ImmutableMap.Builder#build to buildOrThrow #1223

Open
1 of 3 tasks
EnricSala opened this issue Jun 18, 2024 · 0 comments
Open
1 of 3 tasks

Rewrite ImmutableMap.Builder#build to buildOrThrow #1223

EnricSala opened this issue Jun 18, 2024 · 0 comments

Comments

@EnricSala
Copy link

EnricSala commented Jun 18, 2024

Problem

An ImmutableMap.Builder has two terminal methods which accomplish the same thing: build or buildOrThrow.

The buildOrThrow method should be preferred, because the documentation of build specifies:

Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method will throw an exception if there are duplicate keys. The {@code build()} method will soon be deprecated.

Description of the proposed new feature

  • Support a stylistic preference.
  • Avoid a common gotcha, or potential problem.
    • Make the behavior evident: an exception may be thrown (in case of duplicate keys).
  • Improve performance.

I would like to rewrite the following code:

ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
builder.build();

to:

ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
builder.buildOrThrow();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant