CodeGym /ื‘ืœื•ื’ Java /Random-HE /ืฉื™ื˜ืช HashMap computeIfAbsent() ื‘-Java
John Squirrels
ืจึธืžึธื”
San Francisco

ืฉื™ื˜ืช HashMap computeIfAbsent() ื‘-Java

ืคื•ืจืกื ื‘ืงื‘ื•ืฆื”
Java 8, ื›ืคื™ ืฉืจื‘ื™ื ืžืื™ืชื ื• ื™ื•ื“ืขื™ื, ื”ืฆื™ื’ื” ืืช ื”- Stream API. ื–ื•ื”ื™ ืขืจื›ืช ื›ืœื™ื ืฉื™ืžื•ืฉื™ืช ืžืื•ื“, ืืš ื™ืฉ ืœื” ื—ื™ืกืจื•ืŸ, ื”ื™ื ืื™ื ื” ื›ื•ืœืœืช ืžืคื•ืช . ืขื ื–ืืช, Java 8 ื”ื•ืกื™ืคื” ืžืกืคืจ ืฉื™ื˜ื•ืช ืฉื™ืžื•ืฉื™ื•ืช ืœืžืžืฉืง ื”ืžืคื” ืขืฆืžื• ื›ื“ื™ ืœื”ืคื—ื™ืช ืืช ื›ืžื•ืช ื”ืงื•ื“ ื”"ืจืข". ืœื›ืŸ, ืื ืืชื” ืฆืจื™ืš ืœื‘ืฆืข ืคืขื•ืœื” ื›ืœืฉื”ื™ ืขื ื”ืขืจืš ื‘ืžืคื” , ืื ื”ื•ื ืงื™ื™ื ื‘ื•, ื™ืฉ ืฉื™ื˜ื” ComputeIfPresent() ื‘ืžืคืช Java. ืื ืืชื” ืฆืจื™ืš ืœืขืฉื•ืช ืžืฉื”ื• ืขื ืขืจืš ืฉืื™ื ื• ื ืžืฆื ื‘ืžืคื” , ืืชื” ื™ื›ื•ืœ ืœื”ืฉืชืžืฉ ื‘ืฉื™ื˜ืช ComputeIfAbsent() . ื ืฉืงื•ืœ ื–ืืช ื‘ืžืืžืจ ื–ื”.

ื—ืชื™ืžืช ื”ืฉื™ื˜ื” computeIfAbsent().

default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
ืฉื™ื˜ืช Map (ื•- HashMap ) computeIfAbsent() ืœื•ืงื—ืช ืฉื ื™ ืคืจืžื˜ืจื™ื. ื”ืคืจืžื˜ืจ ื”ืจืืฉื•ืŸ ื”ื•ื ื”ืžืคืชื—. ื”ืคืจืžื˜ืจ ื”ืฉื ื™ ื”ื•ื ื”- mappingFunction . ื‘ืฉื™ื˜ื” ื–ื• ืคื•ื ืงืฆื™ื™ืช ื”ืžื™ืคื•ื™ ื ืงืจืืช ืจืง ืื ื”ืžื™ืคื•ื™ ืื™ื ื• ืžื•ืฆื’.

ื›ื™ืฆื“ ืคื•ืขืœืช ืฉื™ื˜ืช computeIfAbsent()

ื›ืคื™ ืฉืื ื• ื›ื‘ืจ ื™ื•ื“ืขื™ื, ืฉื™ื˜ืช Map.computeIfAbsent() ืžื•ืขื‘ืจืช ืฉื ื™ ืคืจืžื˜ืจื™ื, ื”ืžืคืชื— ื•ื”ืคื•ื ืงืฆื™ื” ืœื—ื™ืฉื•ื‘ ื”ืขืจืš ืขื‘ื•ืจ ืžื™ืคื•ื™ ืžืคืชื— ื–ื” . ืœื”ืœืŸ ื”ืืœื’ื•ืจื™ืชื ื”ืœื•ื’ื™ ืฉืœ ื”ืฉื™ื˜ื”:
  1. ื”ืฉื™ื˜ื” ื‘ื•ื“ืงืช ืชื—ื™ืœื” ืื ื”ืžืคืชื— ืฉืขื‘ืจ ืžื™ื•ืฆื’ ื‘ืžืคื” ืฉืœื ื• .
  2. ืื ื”ืžืคืชื— ืžื™ื•ืฆื’ ื‘ืžืคื” ( ื•ื”ื•ื ืœื null), ืื– ื”ืฉื™ื˜ื” ืœื ืขื•ืฉื” ื›ืœื•ื.
  3. ืื—ืจืช, ืื ื”ืžืคืชื— ืื™ื ื• ืžื™ื™ืฆื’ ื‘ืžืคื” ( ืื• ืฉื”ื•ื null), ื”ืฉื™ื˜ื” ืžื—ืฉื‘ืช ืืช ื”ืขืจืš ื‘ืืžืฆืขื•ืช mappingFunction ืœืžืคืชื—.
  4. ืื ื”ืขืจืš ื”ืžืชืงื‘ืœ ืื™ื ื• ืจื™ืง, ื›ืชื•ื‘ ื–ื•ื’ ืžืคืชื—-ืขืจืš ืœืžื™ืคื•ื™.
ื‘ื•ื ื ื›ืชื•ื‘ ืืช ืื•ืชื• ื”ื™ื’ื™ื•ืŸ ื›ืžื• ืงื•ื“:
if (map.get(key) == null)
{
V newValue = mappingFunction.apply(key);
if (newValue != null) map.put(key, newValue);
}

ื“ื•ื’ืžื” ืœืงื•ื“ computeIfAbsent().

ืœื›ืŸ, ืื ื”ืขืจืš ืœื ื ืžืฆื ื‘ืžืคื” , ื”ืฉื™ื˜ื” ืชื‘ืฆืข ืืช ื”ืฉื™ื ื•ื™ื™ื. ื‘ื•ืื• ื ืกืชื›ืœ ืขืœ ื“ื•ื’ืžื” ืคืฉื•ื˜ื”:
import java.util.HashMap;
import java.util.Map;

//map.computeIfAbsent example
public class ComputeIfAbsentExample {

       public static void main(String[] args) {

           Map<String, String> myMap = new HashMap<>();

           myMap.computeIfAbsent("here is my key", key -> key + ", " + "and this is a new value");

           System.out.println(myMap.get("here is my key"));
       }
}
ื”ืคืœื˜ ื”ื•ื:
ื”ื ื” ื”ืžืคืชื— ืฉืœื™, ื•ื–ื” ืขืจืš ื—ื“ืฉ
ืขื›ืฉื™ื• ื‘ื•ืื• ื ืจืื” ืžื” ื”ืฉื™ื˜ื” ืชืขืฉื” ื›ืืฉืจ ื™ืฉ ืขืจืš ื ืชื•ืŸ ื‘ืžืคื” . ื”ืชืจืืช ืกืคื•ื™ืœืจ: ื–ื” ืœื ื™ืขืฉื” ื›ืœื•ื.
import java.util.HashMap;
import java.util.Map;

public class ComputeIfAbsentExample2 {

       public static void main(String[] args) {

           Map<String, String> myMap = new HashMap<>();
           myMap.put("here is my key", "and here is my value");

           myMap.computeIfAbsent("here is my key", key -> key + ", " + "and this is a new value");

           System.out.println(myMap.get("here is my key"));
       }
}
ื”ื ื” ื”ืคืœื˜:
ื•ื”ื ื” ื”ืขืจืš ืฉืœื™
ื›ืคื™ ืฉืืชื” ื™ื›ื•ืœ ืœืจืื•ืช, ื”ืขืจืš ื ืฉืืจ ืœืœื ืฉื™ื ื•ื™.

ืขื•ื“ ื“ื•ื’ืžื” ืื—ืช ืฉืœ ComputeIfAbsent()

ืื ืืชื” ืžื›ื™ืจ ืืช ื”ืžื•ืฉื’ ืžื˜ืžื•ืŸ, ืฉื™ื˜ืช computeIfAbsent() ื›ื ืจืื” ืžื–ื›ื™ืจื” ืœืš ืžืฉื”ื•. ื‘ื•ืื• ื ืกืชื›ืœ ืขืœ ื“ื•ื’ืžื” ืžื•ืจื›ื‘ืช ื™ื•ืชืจ ืฉืœ ื ื™ืชื•ื—. ื‘ื•ืื• ื ืงืจื ืœืžืชื•ื“ื” computeIfAbsent() ืคืขืžื™ื™ื ื›ื“ื™ ืœื•ื•ื“ื ืฉื‘ืžืงืจื” ื”ืจืืฉื•ืŸ ื”ืขืจืš ืžืฉืชื ื”, ื‘ืขื•ื“ ืฉื‘ืžืงืจื” ื”ืฉื ื™ ืœื.
import java.util.HashMap;
import java.util.Map;

public class ComputeIfAbsentExample {
   private static Map<String, Long> numbersMap = new HashMap<>();

   public static Long stringToLong(String str) {
       return numbersMap.computeIfAbsent(str, key -> {
           System.out.println("parsing: " + key);
           return Long.parseLong(key);
       });
   }

   public static void main(String[] args) {
       // will print:
       // > parsing: 10
       // > parsing: 25
       // > 10+25=35
       System.out.println("10+25=" + (stringToLong("10") + stringToLong("25")));
       // will print:
       // > parsing: 20
       // > 10+25=45
       // only "20" will be parsed this time, since "25" was already parsed and placed into `numbersMap` map before
       System.out.println("20+25=" + (stringToLong("20") + stringToLong("25")));
       // will print:
       // > 10+20=30
       // no parsing will occur, since both "10" and "20" were already parsed and placed into `numbersMap` map before
       System.out.println("10+20=" + (stringToLong("10") + stringToLong("20")));
   }

}
ื”ื ื” ื”ืคืœื˜:
ื ื™ืชื•ื—: 10 ื ื™ืชื•ื—: 25 10+25=35 ื ื™ืชื•ื—: 20 20+25=45 10+20=30
ื”ืขืจื•ืช
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION