[JavaStudy] 5. HashMap

์ง„์ฃผยท2022๋…„ 3์›” 13์ผ
0

JavaStudy

๋ชฉ๋ก ๋ณด๊ธฐ
5/9

๐Ÿ‘€ HashMap ?

ํ•ด์‹œ๋งต์€ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•ด์‹ฑ(Hashing)๋œ ๋งต(Map)์ด๋‹ค.

๋งต์€ ํ‚ค(Key)์™€ ๊ฐ’(Value) ๋‘ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.
์—ฌ๊ธฐ์„œ ํ‚ค๋Š” ๋งต์— ์˜ค์ง ์œ ์ผํ•˜๊ฒŒ ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ, ๊ฐ™์€ ๋งต์— ๋™์ผํ•œ ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (๊ฐ’์€ ์ค‘๋ณต๋˜์–ด๋„ ์ƒ๊ด€ ์—†๋‹ค)

Key, Value

๋ฐ์ดํ„ฐ๋“ค์€ ๋ชจ๋‘ (ํ‚ค, ๊ฐ’)์˜ 1:1 ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ๋Š” Entry๋กœ ๋˜์–ด์žˆ๋‹ค.

  • ๊ฐ™์€ ํ‚ค์˜ ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ํ•ด๋‹น ํ‚ค์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ๋‹ค.
  • ํ‚ค๋Š” ๊ณ ์œ ํ•œ ์†์„ฑ์ด์ง€๋งŒ, ๊ฐ’์€ ๊ณ ์œ ํ•œ ์†์„ฑ์ด ์•„๋‹ˆ๋‹ค.
  • ํ‚ค๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š์ง€๋งŒ, ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๋‹ค

๐Ÿ˜™ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

1. HashMap ์„ ์–ธํ•˜๊ธฐ

HashMap์€ Key, Value 2๊ฐœ์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์„ ์„ ์–ธํ•˜๋ ค๋ฉด 2๊ฐœ์˜ ํƒ€์ž…์„ ์„ ์–ธํ•ด์•ผํ•œ๋‹ค.

HashMap< ํƒ€์ž…, ํƒ€์ž… > ๋ณ€์ˆ˜๋ช… = new HashMap<>(); ์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.

[example]

    HashMap hm = new HashMap();
    
    HashMap<Integer, Integer> i = new HashMap<>();
    HashMap<String, String> str = new HashMap<>();
    HashMap<Character, Character> ch = new HashMap<>();

2. ๊ฐ’ ์ถ”๊ฐ€ : put( key, value )

HashMap์˜ ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• : puy(key,value)

  • ๋“ค์–ด๊ฐ€๋Š” ํƒ€์ž…์€ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋‹น์‹œ์˜ ํƒ€์ž…์œผ๋กœ ๋งž์ถฐ ์ž…๋ ฅํ•œ๋‹ค.

  • ๊ฐ™์€ key์˜ ๋ฐ์ดํ„ฐ๋ฅผ putํ•˜๋Š” ๊ฒฝ์šฐ ๋‚˜์ค‘์— ๋„ฃ์€ value๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

[example]

	HashMap<String, String> hm = new HashMap<>();

    hm.put("1","Hello");
    hm.put("2","World");
    hm.put("3","Hello"); // ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๋‹ค!
    System.out.println(hm);

    hm.put("1","Changed"); // key๋Š” ์ค‘๋ณต๋ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ๋‹ค.
    System.out.println(hm);

[๊ฒฐ๊ณผ]


3. ๊ฐ’ ์‚ญ์ œ : remove(key)

HashMap์˜ ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ• : remove(key)

HashMap์˜ ๋ชจ๋“  ํ‚ค ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ• : clear()

[example]

	HashMap<String, String> hm = new HashMap<>();

	hm.put("1","Hello");
	hm.put("2","World");
	hm.put("3","Hello"); // ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๋‹ค!
	System.out.println(hm);

	hm.remove("3");
	System.out.println(hm);

	hm.clear();
	System.out.println(hm);

[๊ฒฐ๊ณผ]


4. ๊ฐ’ ์กฐํšŒ : get(key)

HashMap์—์„œ ๊ฐ’ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ• : get(key)

HashMap<String, String> hm = new HashMap<>();

hm.put("1","Hello");
hm.put("2","World");
hm.put("3","Pure");
hm.put("4","Water");

System.out.println(hm.get("3"));
System.out.println(hm.get("4"));

[๊ฒฐ๊ณผ]


5. ๊ฐ’ ์ถœ๋ ฅ - 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•

1) ํ–ฅ์ƒ๋œ for ๋ฌธ : for(Map.Entry<ํƒ€์ž…, ํƒ€์ž…> ๋ณ€์ˆ˜๋ช… : entrySet())
2) Iterator ํด๋ž˜์Šค ์‚ฌ์šฉ :

[example] ํ–ฅ์ƒ๋œ for๋ฌธ

HashMap<String, String> hm = new HashMap<>();

hm.put("1","Hello");
hm.put("2","World");
hm.put("3","Pure");
hm.put("4","Water");

for(Map.Entry<String,String> s : hm.entrySet()){
   System.out.println("Key : " + s.getKey() + ", Value : " + s.getValue());
}

[๊ฒฐ๊ณผ]


[example2] Iterator ์‚ฌ์šฉ (EntrySet, KeySet ์ด์šฉ)

HashMap<String, String> hm = new HashMap<>();

hm.put("1","Hello");
hm.put("2","World");
hm.put("3","Pure");
hm.put("4","Water");

// ๋ฐฉ๋ฒ• 1 : Iterator๋ฅผ Entry๋กœ ์„ ์–ธ, EntrySet ์ด์šฉ
Iterator<Map.Entry<String,String>> iter = hm.entrySet().iterator();
while(iter.hasNext()){
    Map.Entry<String,String> entry = iter.next();
    System.out.println("Key : " + entry.getKey() + ", Value : " + entry.getValue());
}

System.out.println("ใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…ก");


// ๋ฐฉ๋ฒ• 2 : Iterator๋ฅผ String์œผ๋กœ ์„ ์–ธ, KeySet ์ด์šฉ
Iterator<String> iter2 = hm.keySet().iterator();
while(iter2.hasNext()){
    String key = iter2.next();
    System.out.println("Key : " + key + ", Value : " + hm.get(key));

[๊ฒฐ๊ณผ]


6. key ์œ ๋ฌด ํ™•์ธ : containsKey(key)

๋งต์—์„œ ์ธ์ž๋กœ ๋ณด๋‚ธ key๊ฐ€ ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

[example]

HashMap<String, String> hm = new HashMap<>();

hm.put("1","Hello");
hm.put("2","World");
hm.put("3","Pure");

System.out.println(hm.containsKey("3"));
System.out.println(hm.containsKey("5"));

[๊ฒฐ๊ณผ]


7. Value ์œ ๋ฌด ํ™•์ธ : containsValue(value)

[example]

HashMap<String, String> hm = new HashMap<>();

hm.put("1","Hello");
hm.put("2","World");
hm.put("3","Pure");

System.out.println(hm.containsValue("Pure"));
System.out.println(hm.containsValue("Water"));

[๊ฒฐ๊ณผ]


8. key ์ด ๊ฐœ์ˆ˜ : size()

HashMap<String, String> hm = new HashMap<>();

hm.put("1","Hello");
hm.put("2","World");
hm.put("3","Pure");

System.out.println(hm.size());

[๊ฒฐ๊ณผ]


์ถœ์ฒ˜ : https://hbase.tistory.com/134
์ถœ์ฒ˜ : https://crazykim2.tistory.com/587

profile
์ง„์ฃผ์˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€