Skip to content

MapUtil#computeIfAbsent Object repeated creation #2339

@TAKETODAY

Description

@TAKETODAY

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
  }

Metadata

Metadata

Assignees

Labels

polishingImprove a implementation code or doc without change in current behavior/content

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions