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

Closed
1 of 3 tasks
EnricSala opened this issue Jun 18, 2024 · 1 comment · Fixed by #1488
Closed
1 of 3 tasks

Rewrite ImmutableMap.Builder#build to #buildOrThrow #1223

EnricSala opened this issue Jun 18, 2024 · 1 comment · Fixed by #1488
Milestone

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();
@Stephan202
Copy link
Member

Stephan202 commented Dec 29, 2024

I filed #1488 and #1489 for this.

@Stephan202 Stephan202 changed the title Rewrite ImmutableMap.Builder#build to buildOrThrow Rewrite ImmutableMap.Builder#build to #buildOrThrow Dec 29, 2024
@Stephan202 Stephan202 added this to the 0.20.0 milestone Dec 29, 2024
Stephan202 added a commit that referenced this issue Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants