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() ืืืขืืจืช ืฉื ื ืคืจืืืจืื, ืืืคืชื ืืืคืื ืงืฆืื ืืืืฉืื ืืขืจื ืขืืืจ ืืืคืื ืืคืชื ืื . ืืืื ืืืืืืจืืชื ืืืืื ืฉื ืืฉืืื:- ืืฉืืื ืืืืงืช ืชืืืื ืื ืืืคืชื ืฉืขืืจ ืืืืฆื ืืืคื ืฉืื ื .
- ืื ืืืคืชื ืืืืฆื ืืืคื ( ืืืื ืื null), ืื ืืฉืืื ืื ืขืืฉื ืืืื.
- ืืืจืช, ืื ืืืคืชื ืืื ื ืืืืฆื ืืืคื ( ืื ืฉืืื null), ืืฉืืื ืืืฉืืช ืืช ืืขืจื ืืืืฆืขืืช mappingFunction ืืืคืชื.
- ืื ืืขืจื ืืืชืงืื ืืื ื ืจืืง, ืืชืื ืืื ืืคืชื-ืขืจื ืืืืคืื.
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
GO TO FULL VERSION