๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

Java Map - compute(), computeIfAbsent(), computeIfPresent()

by ์ฃผ๋ฐœ2 2023. 3. 13.
๋ฐ˜์‘ํ˜•

Map ์ธํ„ฐํŽ˜์ด์Šค์—์„œ Java 8 ๋ฒ„์ „์— ์ถ”๊ฐ€๋œ ์ผ๋ถ€ API์— ๋Œ€ํ•ด ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

  1. putIfAbsent()
  2. compute()
  3. computeIfPresent()
  4. computeIfAbsent()
  5. getOrDefault()

ํ•ด๋‹น ์ฝ”๋“œ๋Š” Map ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์ธ HashMap์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

1. putIfAbsent(K key, V value)

Map - putIfAbsent()

putIfAbsent() ๋ฉ”์„œ๋“œ๋Š” ๋„ค์ด๋ฐ์—์„œ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋“ฏ์ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • Key๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ: Value ๋ณ€๊ฒฝ ์—†์ด ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” Key์˜ Value๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
  • Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: Key์— ํ•ด๋‹นํ•˜๋Š” Value๋ฅผ ์ €์žฅํ•œ ํ›„ null์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

 

HashMap - putIfAbsent()

Map์˜ ๊ตฌํ˜„์ฒด์ธ HashMap์„ ๋ณด๋ฉด putVal() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ง„ํ–‰๋˜๊ณ , Key๊ฐ€ ์กด์žฌํ•˜๋ฉด oldValue๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด null์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

 

  • map์— key=20, value=5 ์ธ ๋ฐ์ดํ„ฐ๋ฅผ put ํ•ฉ๋‹ˆ๋‹ค.
  • map.putIfAbsent(20, 10): Key๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— Value์˜ ์ˆ˜์ • ์—†์ด 5๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
  • map.putIfAbsent(10, 5): Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— 5๋ฅผ ์ €์žฅํ•œ ํ›„ null์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

 

 

2. compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

compute() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • Key์˜ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž๋ฅผ ํ†ตํ•˜์—ฌ ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ , ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ key๋ฅผ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด value๋ฅผ ์ €์žฅ
  • Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ NullPointerException์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„ 8๋ฒˆ์งธ ๋ผ์ธ์—์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” Key์— ๋Œ€ํ•ด์„œ๋Š” NullPointerException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ์š”, ์ด๋Š” ๋žŒ๋‹ค ํ•จ์ˆ˜์—์„œ ๋‘ ๋ฒˆ์งธ value๊ฐ€ null๋กœ ์ „๋‹ฌ์ด ๋˜๊ณ , null์ธ ์ƒํƒœ์—์„œ intValue() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•˜๊ธฐ์— NPE ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ์œ„์˜ ๊ฒฝ์šฐ function์˜ ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์ „ value์˜ ๊ฐ’์„ ์ฒดํฌํ•˜์—ฌ ์„ค์ •ํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

 

3. computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction

computeIfAbsent() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • Key๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ: ์•„๋ฌด๋Ÿฐ ์ž‘์—…์„ ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” Key์˜ Value๋ฅผ ๋ฆฌํ„ด
  • Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: ๋žŒ๋‹ค์‹์„ ์ ์šฉํ•œ ๊ฐ’์„ ํ•ด๋‹น key์— ์ €์žฅํ•œ ํ›„ newValue๋ฅผ ๋ฐ˜ํ™˜

 

computeIfAbsent() ๋ฉ”์„œ๋“œ๋Š” ์œ„์—์„œ ์‚ดํŽด๋ณธ putIfAbsent() ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘์„ ํ•ฉ๋‹ˆ๋‹ค.

Key๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” Value๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ €์žฅํ•œ ํ›„ null์„ ๋ฐ˜ํ™˜(putIfAbsent) ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ฐ’(newValue, computeIfAbsent()์„ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ์ฐจ์ด์ ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

 

4. computeIfPresent(K key, Function<? super K, ? extends V> mappingFunction

computeIfPresent() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • Key๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ: ๋žŒ๋‹ค์‹์„ ์ ์šฉํ•œ ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด ํ•ด๋‹น key์— ์ €์žฅํ•œ ํ›„ ๋ฐ˜ํ™˜, null์ด๋ฉด key ์ œ๊ฑฐ ํ›„ null ๋ฐ˜ํ™˜
  • Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: null ๋ฐ˜ํ™˜

 

computeIfPresent() ์˜ˆ์ œ

์œ„ ์ฝ”๋“œ๋Š” key์˜ value๊ฐ€ 1์ด๋ฉด key๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , 1์ด ์•„๋‹Œ ๊ฒฝ์šฐ(1๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ) -1์„ ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋กœ์ง์ž…๋‹ˆ๋‹ค.

 

after ์ฝ”๋“œ์—์„œ๋Š” value๊ฐ€ 1์ผ ๊ฒฝ์šฐ ๋žŒ๋‹ค์‹ ๊ฒฐ๊ณผ๊ฐ’์„ null๋กœ ์„ค์ •ํ•˜๊ณ , 1์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๊ธฐ์กด value - 1์„ ํ•ฉ๋‹ˆ๋‹ค.

(์œ„ computeIfPresent() ๋ฉ”์„œ๋“œ์—์„œ ๋žŒ๋‹ค์‹ ๊ฒฐ๊ณผ๊ฐ’์ด null์ด๋ฉด key๋ฅผ ์ œ๊ฑฐํ•œ ํ›„ null์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.)

 

 

5. getOrDefault(Object key, V defaultValue)

getOrDefault() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • key๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ: key์— ์กด์žฌํ•˜๋Š” value๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • Key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์„ค์ •ํ•œ defaultValue๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

getOrDefault() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ์ธ์ ์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

 

getOrDefault() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด before ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋งค๋ฒˆ Map์— key๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ getOrDefault() ๋ฉ”์„œ๋“œ๋Š” key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ defaultValue๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€