๋ง์ ์ฌ๋๋ค์ด ์๊ณ ์๋ฏ์ด Java 8์ Stream API๋ฅผ ๋์
ํ์ต๋๋ค. ์ด๊ฒ์ ๋งค์ฐ ์ ์ฉํ ํดํท์ด์ง๋ง Map ์ ํฌํจํ์ง ์๋๋ค๋ ๋จ์ ์ด ์์ต๋๋ค . ๊ทธ๋ฌ๋ Java 8์ "์๋ชป๋" ์ฝ๋์ ์์ ์ค์ด๊ธฐ ์ํด Map ์ธํฐํ์ด์ค ์์ฒด์ ๋ช ๊ฐ์ง ์ ์ฉํ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ต๋๋ค. ๋ฐ๋ผ์ Map ์ ์๋ ๊ฐ์ผ๋ก ์ด๋ค ์์
์ ์ํํด์ผ ํ๋ ๊ฒฝ์ฐ Java Map์ ComputeIfPresent() ๋ฉ์๋ ๊ฐ ์์ต๋๋ค . Map ์ ์๋ ๊ฐ์ผ๋ก ์์
์ ์ํํด์ผ ํ๋ ๊ฒฝ์ฐ ComputeIfAbsent() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค . ์ด ๊ธฐ์ฌ์์ ๊ณ ๋ คํ ๊ฒ์
๋๋ค.
computeIfAbsent() ๋ฉ์๋ ์๋ช
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
Map (๋ฐ HashMap ) computeIfAbsent () ๋ฉ์๋๋ ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ํค์
๋๋ค. ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ mappingFunction ์
๋๋ค . ์ด ๋ฉ์๋์์ ๋งคํ ํจ์๋ ๋งคํ์ด ํ์๋์ง ์์ ๊ฒฝ์ฐ์๋ง ํธ์ถ๋ฉ๋๋ค.
computeIfAbsent() ๋ฉ์๋ ์๋ ๋ฐฉ์
์ด๋ฏธ ์๊ณ ์๋ฏ์ด Map.computeIfAbsent() ๋ฉ์๋์๋ ํค์ ์ด ํค mappingFunction ๊ฐ์ ๊ณ์ฐํ๋ ํจ์๋ผ๋ ๋ ๊ฐ์ ๋งค๊ฐ ๋ณ์๊ฐ ์ ๋ฌ๋ฉ๋๋ค . ๋ฐฉ๋ฒ์ ๋ ผ๋ฆฌ์ ์๊ณ ๋ฆฌ์ฆ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.- ๋ฉ์๋๋ ๋จผ์ ์ ๋ฌ๋ ํค๊ฐ Map ์ ํ์๋๋์ง ํ์ธํฉ๋๋ค .
- ํค๊ฐ Map ์ ํ์๋๊ณ null์ด ์๋ ๊ฒฝ์ฐ ๋ฉ์๋๋ ์๋ฌด ์์ ๋ ์ํํ์ง ์์ต๋๋ค.
- ํค๊ฐ Map ์ ํ์๋์ง ์๊ฑฐ๋ (๋๋ null์ธ ๊ฒฝ์ฐ) ๋ฉ์๋๋ ํค์ ๋ํ mappingFunction์ ์ฌ์ฉํ์ฌ ๊ฐ์ ๊ณ์ฐํฉ๋๋ค.
- ๊ฒฐ๊ณผ ๊ฐ์ด null์ด ์๋ ๊ฒฝ์ฐ ํค-๊ฐ ์์ ๋งคํ์ ์๋๋ค.
if (map.get(key) == null)
{
V newValue = mappingFunction.apply(key);
if (newValue != null) map.put(key, newValue);
}
computeIfAbsent() ์ฝ๋ ์์
๋ฐ๋ผ์ ๊ฐ์ด Map ์ ์์ผ๋ฉด ๋ฉ์๋๊ฐ ๋ณ๊ฒฝ์ ์ํํฉ๋๋ค. ๊ฐ๋จํ ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
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"));
}
}
์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ฌ๊ธฐ ๋ด ์ด์ ๊ฐ ์๊ณ ์ด๊ฒ์ ์๋ก์ด ๊ฐ์น์
๋๋ค
์ด์ Map ์ ์ฃผ์ด์ง ๊ฐ์ด ์์ ๋ ๋ฉ์๋๊ฐ ๋ฌด์์ ํ๋์ง ๋ด
์๋ค . ์คํฌ์ผ๋ฌ ๊ฒฝ๊ณ : ์๋ฌด๊ฒ๋ ํ์ง ์์ต๋๋ค.
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