Map<K,V> ์ธํฐํ์ด์ค
- ํค(Key)๋ฅผ ๊ฐ (value)์ ๋งคํ(mapping)ํ๋ ๊ฐ์ฒด
- ๋์ผํ ํค(key)๋ฅผ ๋ ๊ฐ ์ด์ ๋ฑ๋กํ ์ ์๋ค. ์ฆ, ํค(key)๋ ์ค๋ณต์ ํ์ฉ ํ์ง ์๋๋ค.
- ๊ฐ ํค(key)์ ํ๋์ ๊ฐ (value)๋ง ๋งคํ ๋๋ค.
- Map์ ๊ตฌํํ HashMap, TreeMap ๋ฐ LinkedHashMap์ 3๊ฐ์ ๋ฒ์ฉ Map์ ๋์๊ณผ ์ฑ๋ฅ์ Set ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ HashSet, TreeSet ๋ฐ LinkedHashSet๊ณผ ์ ์ฌํ๋ค.
Map.Entry<K,V> ์ธํฐํ์ด์ค
- ๋งต์ ์ํธ๋ฆฌ (key-value pair : ํค์ ๊ฐ์ ํ์ <ํ์ด>)์ด๋ค.
- Map์ ๋ด๋ถ์ ์ผ๋ก Map.Entry<K,Y> ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ Entry<K,Y> ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด์ ์ ์ฅํ๋ ๊ตฌ์กฐ๋ก ์ด๋ฃจ์ด์ ธ์๋ค. ์ฆ, ํค์ ๊ฐ์ ์๋ก ๊ด๋ จ๋ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ ๊ฐ๊ฐ์ ๋ฐฐ์ด๋ก ์ ์ธํ์ง ์๊ณ Entry๋ผ๋ ๋ด๋ถ ํด๋์ค๋ฅผ ์ ์ํ๊ณ , Entry ํ์ ์ ๋ฐฐ์ด์ ์ ์ธํ์ฌ ์ ์ฅํ๋ค.
HashMap<K,V>ํด๋์ค
- ํค(key)๋ฅผ ๊ฐ(value)์ ๋งคํํ์ฌ ์ ์ฅ
- ํค๋ ์ค๋ณต์ ํ์ฉํ์ง ์์ง๋ง ๊ฐ์ ๋ค๋ฅธ ํค๋ก ์ค๋ณต์ ์ธ ์์๋ฅผ ์ ์ฅ ํ ์ ์๋ค.
- ํค๋ ๊ฐ์ null์ ์ฌ์ฉํ ์ ์์ง๋ง ํค์๋ ํ๋์ null๋ง ์ถ๊ฐํ ์ ์๋ค.
- ์์์ ์ ์ฅ์์๋ ์ ์ง๋์ง ์๋๋ค.
- ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์์ ๋๊ธฐํ ๋์ง ์๋๋ค.
LinkedHashMap<K,V> ํด๋์ค
- HashMap<K,V> ํด๋์ค๋ฅผ ์์ ๋ฐ์ ํด๋์ค
- ํด์ ํ ์ด๋ธ๊ณผ ๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ๊ตฌํํ ๋งต
- ํค๋ฅผ ๊ฐ์ ๋งคํํ์ฌ ์ ์ฅ
- ์ฝ์ ํ๋ ์์๋๋ก ์ ์ฅ๋๋ค.
- ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์์ ๋๊ธฐํ ๋์ง ์๋๋ค.
Hashtable<K,Y>ํด๋์ค
- ํค๋ฅผ ๊ฐ์ ๋งคํํ์ฌ ์ ์ฅ
- ํค๋ ์ค๋ณต์ ํ์ฉํ์ง ์์ง๋ง ๊ฐ์ ์ค๋ณต์ ์ธ ์์๋ฅผ ์ ์ฅ ํ ์ ์๋ค.
- null ์ด์ธ์ ๊ฐ์ฒด๋ ํค ๋๋ ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค. ํค๋ ๊ฐ์ null์ ์ฌ์ฉํ๋ฉด ๋ฐํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
- ์์์ ์ ์ฅ์์๋ ์ ์ง๋์ง ์๋๋ค.
- ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์์ ๋๊ธฐํ ๋๋ค.
TreeMap<K,Y> ํด๋์ค
- SortedMap ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค
- ํค๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋์ด ์ ์ฅ๋๋ค.
- ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์์ ๋๊ธฐํ ๋์ง ์๋๋ค.
- TreeMap์ ํค๋ Comparable ์ธํฐํ์ด์ค์ compareTO() ๋ฉ์๋๋ Comparator ์ธํฐํ์ด์ค์ compare() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๋ชจ๋ ์์๋ฅผ ๋น๊ตํ๋ค. ๋ฐ๋ผ์ TreeMap์ key๋ Comparable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค์ ๊ฐ์ฒด์ด๊ฑฐ๋ Comparator๋ฅผ ๊ตฌํํ ๊ฐ์ฒด๋ฅผ TreeMap ์์ฑ์์ ์ธ์๋ก ๋๊ฒจ ๋ ๊ฐ์ฒด์ ๋น๊ต ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ผ ํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ClassCastException ์ด๋ผ๋ ๋ฐํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
Properties ํด๋์ค
- Hashtable ํด๋์ค์ ํ์ ํด๋์ค
- ํค์ ๊ฐ ๋ชจ๋ String์ผ๋ก ์ ํํ Map
- ํ์ผ ์ ์ถ๋ ฅ์ ์ง์ํ๋ค
package ch11.unit05;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/*
- Map
: ํค, ๊ฐ ๊ตฌ์กฐ
: ํค๋ ์ค๋ณต ํ์ฉ ์ํจ
: ํค๋ ์์ ์์
- ๊ตฌํ ํด๋์ค
HashMap : ๋๊ธฐํ ์ง์ ์ํจ
Hashtable : ๋๊ธฐํ ์ง์
LinkedHashMap : ํค๊ฐ ๋ฑ๋ก๋ ์์ ์ ์ง
TreeMap : ํค ์์๋ก ์ ๋ ฌ. ํค๋ Comparable ์ธํฐํ์ด์ค๊ฐ ๊ตฌํ๋์ด ์์ด์ผ ํจ
- ํค๋ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ. ๋ณ๊ฒฝํ ๊ฒฝ์ฐ ์ญ์ ํ ์ฌ๋ฑ๋ก
- ๊ฐ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ : replace(ํค,๊ฐ) ์ด์ฉ
*/
public class Ex01 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
// put(ํค,๊ฐ) : Map์ ๊ฐ ์ ์ฅ
map.put("์์ธ", 1000);
map.put("๋ถ์ฐ", 350);
map.put("๋๊ตฌ", 250);
map.put("์ธ์ฒ", 300);
map.put("๊ด์ฃผ", 150);
map.put("๋์ ", 150);
map.put("์ธ์ฐ", 110);
map.put("์ธ์ข
", 20);
map.put("์์ธ", 990); // ํค๊ฐ ๊ฐ์ผ๋ฉด ๊ธฐ์กด ๊ฐ์ ๋ฎ์ด์
System.out.println(map);
//get(ํค) : Map์์ ํค์ ๋ํ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
int a = map.get("์์ธ");
System.out.println(a);
//int b= map.get("๊ฒฝ๊ธฐ"); // ๋ฐํ์ ์ค๋ฅ
Integer b = map.get("๊ฒฝ๊ธฐ"); // ์์ผ๋ฉด null
System.out.println(b);
System.out.println();
// Map์ Iterator๊ฐ ์์ผ๋ฉด ํฅ์๋ for๋ฌธ๋ ์ฌ์ฉ ๋ถ๊ฐ
// Set<String> set = map.keySet(); ๋ ํค์์
// Set ๊ฐ์ฒด๋ฅผ ๋ฐํ → ํค๋ฅผ ์ฒ์๋ถํฐ ๋ง์ง๋ง๊น์ง ์ํ
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()) {
String key = it.next();
Integer value = map.get(key);
System.out.println(key+":"+value);
}
System.out.println();
for(String key : map.keySet()) {
Integer value = map.get(key);
System.out.println(key+":"+value);
}
System.out.println();
System.out.println("ํค์ ์์ธ ์กด์ฌ?"+map.containsKey("์์ธ"));
System.out.println("ํค์ ๊ฒฝ๊ธฐ ์กด์ฌ?"+map.containsKey("๊ฒฝ๊ธฐ"));
System.out.println("ํค์ 350 ์กด์ฌ?"+map.containsValue(350));
System.out.println("์ ์ฒด๊ฐ์:"+map.size());
map.remove("์ธ์ข
"); // ์ญ์
System.out.println(map);
}
}
package ch11.unit05;
import java.util.Map;
import java.util.TreeMap;
public class Ex03 {
public static void main(String[] args) {
// ํค ์์๋ก ์ ๋ ฌ
Map<String, Integer>map = new TreeMap<String, Integer>();
map.put("์๋ฐ", 80);
map.put("์ค๋ผํด", 90);
map.put("๋น
๋ฐ์ดํฐ", 85);
map.put("์๋ธ๋ฆฟ", 100);
map.put("์คํ๋ง", 80);
System.out.println(map);
}
}
package ch11.unit05;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Ex04 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("์๋ฐ", 80);
map.put("์ค๋ผํด", 90);
map.put("๋น
๋ฐ์ดํฐ", 85);
map.put("์๋ธ๋ฆฟ", 100);
map.put("์คํ๋ง", 80);
/*
- entry
: ํค์ ๊ฐ์ set ํํ๋ก ์ ์ฅ
: ํค์ ๊ฐ์ ๋ฌถ์ด์ฃผ์ง ์ํด ์ฌ์ฉ
- Map.Entry
: Map์ ์ํธ๋ฆฌ
*/
Set<Map.Entry<String, Integer>> set = map.entrySet();
Iterator<Map.Entry<String, Integer>> it = set.iterator();
while(it.hasNext()) {
Map.Entry<String, Integer> e = it.next();
System.out.println(e.getKey()+","+e.getValue());
}
}
}
package ch11.unit05;
import java.util.Iterator;
import java.util.Properties;
/*
* Properties
: Hashtable์ ์์ ๋ฐ์
: ํค์ ๊ฐ์ด ๋ชจ๋ ๋ฌธ์์ด(String)๋ง ๊ฐ๋ฅ
: ์ฃผ์ ๋ฉ์๋
setProperty("ํค", "๊ฐ") : Properties์ ๊ฐ ์ ์ฅ
getProperty("ํค") : Properties์ ํค์ ๋ํ ๊ฐ ๋ฐํ
*/
public class Ex05_Properties {
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("์๋ฐ", "100");
p.setProperty("์คํ๋ง", "80");
p.setProperty("์ค๋ผํด", "95");
p.setProperty("์๋ธ๋ฆฟ", "85");
System.out.println(p);
//System.out.println(p);
p.list(System.out);
System.out.println("-----------------------");
// Properties์ ์ ์ฅ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
String k ="์๋ฐ",s;
s =p.getProperty(k);
System.out.println(s);
//์ ์ฒด ์ถ๋ ฅ
Iterator<Object>it = p.keySet().iterator();
while(it.hasNext()) {
String key = (String)it.next();
String value = p.getProperty(key);
System.out.println(key+"-"+value);
}
}
}
package ch11.unit05;
import java.io.FileOutputStream;
import java.util.Properties;
public class Ex06 {
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("์๋ฐ", "100");
p.setProperty("์คํ๋ง", "80");
p.setProperty("์ค๋ผํด", "95");
p.setProperty("์๋ธ๋ฆฟ", "85");
p.setProperty("HTML", "100");
// Properties์ ์ ์ฅ๋ ๋ด์ฉ์ ํ์ผ์ ์ ์ฅ
String pathname ="subject.properties";
try (FileOutputStream fos = new FileOutputStream(pathname)) {
// ํ์ผ์ ์ ์ฅ
p.store(fos, "๊ณผ๋ชฉ๋ณ ์ฑ์ ");
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(33) [JAVA] 18์ผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ(I/O Stream) (0) | 2024.04.03 |
---|---|
[Java] 17์ผ์ฐจ : ์ปฌ๋ ์ (Collection) - 4 (0) | 2024.03.11 |
[Java] 16์ผ์ฐจ : ์ปฌ๋ ์ (Collection) - 2 (0) | 2024.03.11 |
[Java] 16์ผ์ฐจ : ์ปฌ๋ ์ (Collection) (0) | 2024.03.11 |
[Java] 15์ผ์ฐจ : ์ ๋๋ฆญ(Generics) (0) | 2024.03.06 |