ํด์๋งต์ ์ด๋ฆ ๊ทธ๋๋ก ํด์ฑ(Hashing)๋ ๋งต(Map)์ด๋ค.
๋งต์ ํค(Key)์ ๊ฐ(Value) ๋ ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
์ฌ๊ธฐ์ ํค๋ ๋งต์ ์ค์ง ์ ์ผํ๊ฒ ์์ด์ผ ํ๋๋ฐ, ๊ฐ์ ๋งต์ ๋์ผํ ํค๊ฐ ์กด์ฌํ๋ฉด ์๋๋ค๋ ๊ฒ์ด๋ค. (๊ฐ์ ์ค๋ณต๋์ด๋ ์๊ด ์๋ค)
๋ฐ์ดํฐ๋ค์ ๋ชจ๋ (ํค, ๊ฐ)์ 1:1 ๊ตฌ์กฐ๋ก ๋์ด์๋ Entry๋ก ๋์ด์๋ค.
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<>();
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);
[๊ฒฐ๊ณผ]
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);
[๊ฒฐ๊ณผ]
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"));
[๊ฒฐ๊ณผ]
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));
[๊ฒฐ๊ณผ]
๋งต์์ ์ธ์๋ก ๋ณด๋ธ 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"));
[๊ฒฐ๊ณผ]
[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"));
[๊ฒฐ๊ณผ]
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