CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /Java์—์„œ ๋งต์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

Java์—์„œ ๋งต์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
์ด ๊ฒŒ์‹œ๋ฌผ์—์„œ๋Š” Java์—์„œ Map์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•๊ณผ ์˜ˆ๋ฅผ ํ†ตํ•ด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— Java์˜ ์ธํ„ฐํŽ˜์ด์Šค ์™€ ๋งต์„ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๊ธฐ์— ๊ฐ„๋‹จํ•œ ์š”์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Java์—์„œ ๋งต์ด๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์ด์— ๋Œ€ํ•œ ํ•ด์„์€ ๋‹ค์–‘ํ•˜์ง€๋งŒ ๊ฐ„๋‹จํžˆ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•ด ๋ณด์ž.
"์ง€๋„๋Š” ํ‚ค-๊ฐ’ ์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” Java์˜ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค."
์ง€๋„๊ฐ€ ๋ฌด์—‡์— ์‚ฌ์šฉ๋˜๋Š”์ง€ ์ƒ๊ฐํ•˜๊ณ  ์žˆ๋‹ค๋ฉด? ์•„๋‹ˆ๋ฉด ์–ธ์ œ ํ•„์š”ํ•ฉ๋‹ˆ๊นŒ? ๊ทธ๋Ÿฐ ๋‹ค์Œ ์‹ค์ƒํ™œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ์ €์žฅํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์ „์—์„œ ํ•œ ๊ธ€์ž์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๋‹จ์–ด๋Š” Java์—์„œ ๋งต ํ˜•ํƒœ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ผ€์ด ์—ฐ, ์™•, ํ•œ๊ตญ, ๊ธฐ์‚ฌ, ... ๋“ฑ
์—˜ ์šฉ์•”, ์ƒ๋ช…, ๋น›, ์‚ฌ๋ž‘, ๋ ˆ๋ฐ”๋…ผ, ... ๋“ฑ
๋˜ํ•œ ๋‹ค์Œ ์˜ˆ๋ฅผ ๋ณด๋ฉด ์ด๋ฅผ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์—ด์‡  ๊ฐ’
๊ฐ€์กฑ ID ๊ฐ€์กฑ ๊ตฌ์„ฑ์›๋“ค
ํด๋ž˜์Šค ์ด๋ฆ„ ํ•™์ƒ์ฆ
์ง€์—ญ ์ด๋ฆ„ ์šฐํŽธ๋ฒˆํ˜ธ
์˜์—ญ ๋ธ”๋ก ์ง‘ ๋ฒˆํ˜ธ

๋งต์„ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค, ์ˆ˜์ • ๋˜๋Š” ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด ๋งต์„ ์ˆœํšŒํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์ง„ ๋ช‡ ๊ฐ€์ง€ ์˜ต์…˜์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Java์—์„œ ๋งต์„ ๋ฐ˜๋ณตํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋งต์„ ํšก๋‹จํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ€์žฅ ํšจ์œจ์ ์ด๊ณ  ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์— ์ค‘์ ์„ ๋‘˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  1. ForEach ๋ฃจํ”„ ๋ฐฉ๋ฒ•
  2. ๋ฐ˜๋ณต์ž ๋ฐฉ๋ฒ•
์•„๋ž˜์—์„œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์˜ ๊ตฌํ˜„์„ ์ฐพ์œผ์‹ญ์‹œ์˜ค.

Foreach ๋ฃจํ”„ ๋ฐฉ๋ฒ• ์‚ฌ์šฉ

์˜ˆ


import java.util.Map;
import java.util.HashMap;

public class ForEachDemo {

	public static void main(String[] args) {

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

		// store business days i-e; key/value pairs in the Map
		businessDays.put("1", "Monday");
		businessDays.put("2", "Tuesday");
		businessDays.put("3", "Wednesday");
		businessDays.put("4", "Thursday");
		businessDays.put("5", "Friday");

		// Iterating over the Map.entrySet() using map.forEach
		for (Map.Entry<String, String> entry : businessDays.entrySet()) 
		{
			System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue()); 
		}
	}
}

์‚ฐ์ถœ

ํ‚ค = 1, ๊ฐ’ = ์›”์š”์ผ ํ‚ค = 2, ๊ฐ’ = ํ™”์š”์ผ ํ‚ค = 3, ๊ฐ’ = ์ˆ˜์š”์ผ ํ‚ค = 4, ๊ฐ’ = ๋ชฉ์š”์ผ ํ‚ค = 5, ๊ฐ’ = ๊ธˆ์š”์ผ

์„ค๋ช…

์ด ์˜ˆ์ œ์—์„œ๋Š” foreach ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งต์„ ๋ฐ˜๋ณตํ–ˆ์Šต๋‹ˆ๋‹ค. for-each ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค-๊ฐ’ ์Œ์˜ ํ˜•ํƒœ๋กœ ๋งต์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ž๋™ํ™”๋œ "๋ณด๊ธฐ"๋ฅผ ์ œ๊ณตํ•˜๋Š” entrySet()์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ฐ entrySet์—๋Š” ํ‚ค์™€ ํ•ด๋‹น ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ Map.Entry<key, value> ์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์—ฌ๊ธฐ์„œ๋Š” ์ฝ˜์†”์— ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด getKey() ๋ฐ getValue() ๋งŒ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค . ์—ฐ์Šต์œผ๋กœ ์ด ๊ฐœ๋…์— ๋Œ€ํ•œ ๋ช…๋ น์„ ๊ฐ•ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋‚˜๋จธ์ง€ ๋ฐฉ๋ฒ•์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋ณต์ž ๋ฐฉ๋ฒ• ์‚ฌ์šฉ

์˜ˆ


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapIterator {

	public static void main(String[] args) {

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

		// store business days i-e; key/value pairs in the Map
		monthsInAYear.put("1", "January");
		monthsInAYear.put("2", "February");
		monthsInAYear.put("3", "March");
		monthsInAYear.put("4", "April");
		monthsInAYear.put("5", "May");
		monthsInAYear.put("6", "June");
		monthsInAYear.put("7", "July");
		monthsInAYear.put("8", "August");
		monthsInAYear.put("9", "September");
		monthsInAYear.put("10", "October");
		monthsInAYear.put("11", "November");
		monthsInAYear.put("12", "December");

		// iterate map / traverse the map using using iterator
		Iterator<Map.Entry<String, String>> iterator = monthsInAYear.entrySet().iterator();

		while (iterator.hasNext()) 
		{
			// check if next entry exists in the map
			Map.Entry<String, String> entry = iterator.next(); 
			System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());

		}
	}
}

์‚ฐ์ถœ

ํ‚ค = 11, ๊ฐ’ = 11์›” ํ‚ค = 1, ๊ฐ’ = 1์›” ํ‚ค = 12, ๊ฐ’ = 12์›” ํ‚ค = 2, ๊ฐ’ = 2์›” ํ‚ค = 3, ๊ฐ’ = 3์›” ํ‚ค = 4, ๊ฐ’ = 4์›” ํ‚ค = 5, ๊ฐ’ = 5์›” ํ‚ค = 6, ๊ฐ’ = 6์›” ํ‚ค = 7, ๊ฐ’ = 7์›” ํ‚ค = 8, ๊ฐ’ = 8์›” ํ‚ค = 9, ๊ฐ’ = 9์›” ํ‚ค = 10, ๊ฐ’ = 10์›”

์„ค๋ช…

์ด ์˜ˆ์ œ์—์„œ๋Š” ๋งต์„ ์ˆœํšŒ/๋ฐ˜๋ณตํ•  ๋ช…์‹œ์  ๋ฐ˜๋ณต์ž๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋จผ์ € ๋ฐ˜๋ณต์ž ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ entrySet์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ง€๋„์— ์กด์žฌํ•˜๋Š” ๋‹ค์Œ ์—”ํ„ฐํ‹ฐ๋ฅผ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ์ง€๋„๋ฅผ ํšก๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ž! ์ˆœํšŒ๊ฐ€ ์ด๋ ‡๊ฒŒ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Foreach ๋ฃจํ”„๊ฐ€ ๋ฐ˜๋ณต์ž ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋‚ซ์Šต๋‹ˆ๊นŒ?

์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•œ ๋งต ์ˆœํšŒ์— ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š”์ง€๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์„ฑ๋Šฅ ์ธก๋ฉด์—์„œ for-each ๋ฃจํ”„์™€ ๋ฐ˜๋ณต์ž๋Š” ๋ชจ๋‘ ๋™์ผํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค . ๋”ฐ๋ผ์„œ ์–ด๋Š ๊ฒƒ๋„ ๋‹ค๋ฅธ ๊ฒƒ๋ณด๋‹ค ๋‚ซ์ง€ ์•Š์œผ๋ฉฐ, ์–ธ์ œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

๋‘ ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

for-each ๋ฃจํ”„๋Š” ๋งต์˜ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ/์ˆ˜์ •์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. iterators ํด๋ž˜์Šค๋Š” ๋งต์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํŽธ์ง‘/์ œ๊ฑฐํ•˜๋Š” ๊ตฌํ˜„์„ ์ œ๊ณตํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ for-each ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€๋„ ๋ฐ์ดํ„ฐ๋ฅผ ํŽธ์ง‘/์‚ญ์ œํ•˜๋ ค๊ณ  ํ•˜๋ฉด ConcurrentModificationException ์ด ๋ฐœ์ƒ ํ•ฉ๋‹ˆ๋‹ค . ๊ทธ ์ด์œ ๋Š” for-each ๋ฃจํ”„๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ์•”์‹œ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ์•ก์„ธ์Šค ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.

์–ด๋–ค ์ˆœํšŒ ๋ฐฉ๋ฒ•์„ ์–ธ์ œ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๊ฐ€?

๋งต์„ ๋ฐ˜๋ณตํ•˜๊ธฐ ์œ„ํ•ด for-each ๋˜๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ• ์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋‹ค์Œ ์ œ์•ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋งต์„ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค .
  • ์ค‘์ฒฉ๋œ ๋ฃจํ”„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ for-each ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค (๋ณต์žก์„ฑ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด).

๊ฒฐ๋ก 

๊ฒŒ์‹œ๋ฌผ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ Java์—์„œ ๋งต์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ด๋ฅผ ์—ฐ์Šตํ•˜๊ณ  ๋‹ค๋ฅธ ์ˆœํšŒ ๋ฐฉ๋ฒ•๋„ ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ง‰ํžŒ ๋Š๋‚Œ์ด ๋“ค ๋•Œ๋งˆ๋‹ค ์–ธ์ œ๋“ ์ง€ ๋’ค๋กœ ๋Œ์•„๊ฐ€๊ฑฐ๋‚˜ ์งˆ๋ฌธ์„ ๊ฒŒ์‹œํ•˜์„ธ์š”. ๊ทธ๋•Œ๊นŒ์ง€, ํ–‰๋ณตํ•œ ํ•™์Šต!
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION