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

MapUtil#computeIfAbsent Object repeated creation #2339

Closed
TAKETODAY opened this issue Sep 15, 2021 · 4 comments
Closed

MapUtil#computeIfAbsent Object repeated creation #2339

TAKETODAY opened this issue Sep 15, 2021 · 4 comments
Assignees
Labels
polishing Improve a implementation code or doc without change in current behavior/content
Milestone

Comments

@TAKETODAY
Copy link
Contributor

return map.computeIfAbsent(key, mappingFunction::apply);

Present version

  public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Function<K, V> mappingFunction) {
    V value = map.get(key);
    if (value != null) {
      return value;
    }
    return map.computeIfAbsent(key, mappingFunction::apply); // Object repeated creation
  }

Fix

  public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Function<K, V> mappingFunction) {
    V value = map.get(key);
    if (value != null) {
      return value;
    }
    return map.computeIfAbsent(key, mappingFunction); // Pass parameters directly
  }
@TAKETODAY
Copy link
Contributor Author

Did I understand it wrong? ? ?

@harawata
Copy link
Member

harawata commented Sep 15, 2021

Oh, you are right.
Would you care to send us a pull request?
If you are busy, that is okay. I'll fix it later.

@TAKETODAY
Copy link
Contributor Author

Yes

@harawata
Copy link
Member

PR merged. Thank you!

@harawata harawata self-assigned this Sep 16, 2021
@harawata harawata added the polishing Improve a implementation code or doc without change in current behavior/content label Sep 16, 2021
@harawata harawata added this to the 3.5.8 milestone Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
polishing Improve a implementation code or doc without change in current behavior/content
Projects
None yet
Development

No branches or pull requests

2 participants