Map ์ธํฐํ์ด์ค
๐กMap ์ธํฐํ์ด์ค๋ ํ๋๊ฐ ์๋ ์(pair)๋ก ๋์ด ์๋ ์๋ฃ๋ฅผ ๊ด๋ฆฌํ๋ ๋ฉ์๋๋ก ์ ์ธ๋์ด ์์ต๋๋ค.
Map์ ํคkey์ ๊ฐvalue๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
ํคkey๋ ์ค๋ณต ํ์ฉ์ด ์๋๊ณ , ๊ฐvalue๋ ์ค๋ณต์ด ๊ฐ๋ฅํฉ๋๋ค.
Hash?
๋ฐ์ดํฐ์ ๋ํ ๊ฒ์๊ณผ ์ ์ฅ์์ ์์ฃผ ์ฐ์ํ ์ฑ๋ฅ์ ๋ณด์ด๋ฉฐ, ์ฐ์์๊ฐ ๋ง์ต๋๋ค.
Hash๋ก ๊ฐ์ ์์ฑํ๋ฉด ๊ณ ์ ์ฃผ์ ๊ฐ์ด ์๊ธฐ๋๋ฐ ์ด๊ฒ์ ์ซ์๋ก ๋ณํํ ๊ฒ์ hashCode๋ผ๊ณ ํฉ๋๋ค.
์ฆ, ์๋ฐ์์ ํด์์ฝ๋๋ Heap ์์ญ์ ์ธ์คํด์ค์ ๋ํ ์ฐธ์กฐ ๊ฐ์ด๋ผ๊ณ ๋ ๋ถ๋ฅผ ์ ์์ต๋๋ค.
Hash ํจ์๋?
๋ฐ์ดํฐ์ ํจ์จ์ ์ธ ๊ด๋ฆฌ๋ฅผ ์ํด ์์์ ๊ธธ์ด๋ฅผ ๊ฐ์ง ๋ฐ์ดํฐ๋ฅผ ๊ณ ์ ๋ ๊ธธ์ด๋ฅผ ๊ฐ์ง ๋ฐ์ดํฐ๋ก ๋งคํํด์ฃผ๋ ํจ์
์ํธํ๊ธฐ๋ฒ์ด๋ผ๊ณ ๋ ๋ณผ ์ ์์ต๋๋ค.
Hash Collision?
๊ฐ์ฅ ์ด์์ ์ธ Hashtable์ ํ ๋ฒ์ผ์ ๋ฑ ํ๊ฐ์ ๋ ธ๋๊ฐ ์๋ ๊ฒฝ์ฐ ์ ๋๋ค.
์ด ๋ ๋ฐ์ดํฐ ๊ฒ์์ ํ ๊ฒฝ์ฐ ์ธ๋ฑ์ค๋ง ๊ณ์ฐํ๋ฉด ๋ฐ๋ก ๊ฐ์ ์ฐพ์ ์ ์๊ธฐ ๋๋ฌธ์ O(1)์ ์์๋ณต์ก๋๋ฅผ ๋ณด์ฅ ํ ์ ์์ต๋๋ค.
์ด ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ ํ ๋ฒ์ผ์ ์ฌ๋ฌ๊ฐ์ ๋ ธ๋๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์ถฉ๋์ด๋ผ๊ณ ํฉ๋๋ค.
Map ์ปฌ๋ ์
Map์ ๋ฆฌ์คํธ๋ ๋ฐฐ์ด์ฒ๋ผ ์์ฐจ์ ์ผ๋ก ํด๋น ์์ ๊ฐ์ ๊ตฌํ์ง ์๊ณ key๋ฅผ ํตํด value๋ฅผ ์ป์ต๋๋ค.
- ์์๊ฐ ๋ณด์ฅ ๋์ง ์์
- key์ value๋ก ๊ตฌ์ฑ๋์ด ์์ผ๋ฉฐ key๋ ์ค๋ณต ํ์ฉํ์ง ์๊ณ value๋ ์ค๋ณต ๊ฐ๋ฅ
- key์ ๋์ผ ์กฐ๊ฑด์ hashCode()์ ๋ฆฌํด ๊ฐ์ด ๊ฐ๊ณ , equals() ๋ฉ์๋ ๊ฒฐ๊ณผ๊ฐ true
HashMap
- ์์์ ์ค๋ช ํ ๊ฒ ์ฒ๋ผ hashCode() ๋ฆฌํด๊ฐ๊ณผ equals() ๋ฆฌํด ๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ ๋์ผ ๊ฐ์ฒด๋ก ๊ฐ์ฃผ ํฉ๋๋ค.
- ๋ฐ๋ผ์, hashCode()์ equals() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํด์ ์ฌ์ฉํ ๋๋ ์์ต๋๋ค.
- HashMap์ ์ฑ๋ฅ์ด ์ฐ์ํ๊ณ , ๋ฉํฐ ์ค๋ ๋ ํ๊ฒฝ์ ์ง์ํ์ง ์์ต๋๋ค.(๋๊ธฐํ ๊ธฐ๋ฅ X)
๊ธฐ๋ณธ ์์
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
// ๊ฐ์ฒด ์ถ๊ฐ : put(K,V)
map.put("์๋ฉ๋ฆฌ์นด๋
ธ", 3000);
map.put("๋ผ๋ผ", 3500);
// ๊ฐ์ฒด ์ฐพ๊ธฐ : get(K) - return V
System.out.println("๊ฐ ์ฝ๊ธฐ : " + map.get("์๋ฉ๋ฆฌ์นด๋
ธ"));
// ๊ฐ์ฒด ์ญ์ : remove(K) - return ์ญ์ ํ K์ V
System.out.println("๊ฐ ์ ๊ฑฐ : " + map.remove("๋ผ๋ผ"));
// ์๋ ๊ฐ์ ์ถ๊ฐ/์ญ์ ํ๋ฉด null ๊ฐ ๋ฐํ
System.out.println("๊ฐ ์ฝ๊ธฐ : " + map.get("๋์ฒด๋ผ๋ผ"));
System.out.println("๊ฐ ์ ๊ฑฐ : " + map.remove("๋์ฒด๋ผ๋ผ"));
}
}
๊ธฐ๋ณธ ์์
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("์์ฝฉ๋ฏธ", 99);
map.put("ํ๊ธธ๋", 55);
map.put("ํ์๋ด", 80);
map.put("์ด์์ ", 96);
map.put("ํ๊ธธ๋", 88);
System.out.println("์ด Entry ์ : " + map.size()); // 4
// ๊ฐ์ฒด ์ฐพ๊ธฐ (List๋ ์ธ๋ฑ์ค๋ก ์ฐพ์ง๋ง Map์ ํค ๊ฐ์ผ๋ก ์ฐพ๋๋ค.)
System.out.println(map.get("ํ๊ธธ๋")); // ํค๊ฐ ๊ฐ๊ธฐ ๋๋ฌธ์ ๊ฐ์ด ๋์ค์ ๊ฐ์ผ๋ก ๋์ฒด๋จ
// Map์ ์ํํ๋ ๋ฐฉ๋ฒ : ํฅ์๋ for๋ฌธ ์ฌ์ฉ
for(String key : map.keySet()) { // ๋ชจ๋ ํค๋ฅผ Set ๊ฐ์ฒด์ ๋ด์์ ๋ฆฌํด
System.out.println(key + " : " + map.get(key));
}
// Map์ ์ํํ๋ ๋ฐฉ๋ฒ 2 : ๋ฐ๋ณต์๋ก ์ํํ๋ ๋ฐฉ๋ฒ(iterator)
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) { // ํค๊ฐ ๋ค์์ ์๋์ง ํ์ธ ( ๋ค์์ ํค๊ฐ ์กด์ฌํ๋์ง ํ์ธ )
String key = iterator.next(); // ํค๋ฅผ ๊ฐ์ ธ์ด
System.out.println(key + " : " + map.get(key));
}
// ๊ฐ์ฒด ์ญ์
map.remove("ํ๊ธธ๋"); // ํค๋ก Map.Entry๋ฅผ ์ ๊ฑฐ
System.out.println("์ด Entry ์ : " + map.size());
// ๊ฐ์ ์์ ํ๋ ๋ฉ์๋
map.replace("์ด์์ ", 91);
// Map.Entry ์ ์ฒด ์ญ์
map.clear();
System.out.println("์ด Entry ์ : " + map.size());
}
}
์์ฉ ์์ (์นดํ ๋ฉ๋ด ๋ง๋ค๊ธฐ)
public class MenuInfo{
String name; // ๋ฉ๋ด๋ช
int price; // ๊ฐ๊ฒฉ
String group; // ๋ถ๋ฅ
String desc; // ์ค๋ช
public MenuInfo(String name, int price, String group, String desc) {
this.name = name;
this.price = price;
this.group = group;
this.desc = desc;
}
}
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
// [1] ๋ฉ๋ด๋ณด๊ธฐ [2] ๋ฉ๋ด์กฐํ [3] ๋ฉ๋ด์ถ๊ฐ [4]๋ฉ๋ด์ญ์ [5]๋ฉ๋ด์์ [6]์ข
๋ฃ
public class Main {
Map<String, MenuInfo> map = new TreeMap<>();
public static void main(String[] args) {
Main coffee = new Main();
coffee.makeMenu();
coffee.selectMenu();
}
void makeMenu() {
map.put("์๋ฉ๋ฆฌ์นด๋
ธ", new MenuInfo("์๋ฉ๋ฆฌ์นด๋
ธ", 3500, "์ปคํผ", "์์ธํ ๋ง"));
map.put("๋ผ๋ผ", new MenuInfo("๋ผ๋ผ", 4000, "์ปคํผ", "๊ณ ์ํ ๋ง"));
map.put("์นด๋ผ๋ฉ๋ง๋ผ์๋", new MenuInfo("์นด๋ผ๋ฉ๋ง๋ผ์๋", 4300, "์ปคํผ", "๋ฌ๋ฌํ ๋ง"));
}
void selectMenu() {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("๋ฉ๋ด๋ฅผ ์ ํ ํ์ธ์.");
System.out.print("[1] ๋ฉ๋ด๋ณด๊ธฐ [2] ๋ฉ๋ด์กฐํ [3] ๋ฉ๋ด์ถ๊ฐ [4]๋ฉ๋ด์ญ์ [5]๋ฉ๋ด์์ [6]์ข
๋ฃ : ");
int selMenu = sc.nextInt();
String menu = "";
switch(selMenu) {
case 1 :
System.out.println("======== ๋ฉ๋ด ๋ณด๊ธฐ ========");
for(String key : map.keySet()) {
System.out.println("๋ฉ๋ด : " + map.get(key).name);
System.out.println("๊ฐ๊ฒฉ : " + map.get(key).price);
System.out.println("๋ถ๋ฅ : " + map.get(key).group);
System.out.println("์ค๋ช
: " + map.get(key).desc);
System.out.println("--------------------------");
}
break;
case 2 :
System.out.print("์กฐํํ ๋ฉ๋ด ์ ํ : ");
menu = sc.next();
if(map.containsKey(menu)) { // map์ ํค๊ฐ ์กด์ฌํ๋์ง ํ์ธ (boolean)
System.out.println("๋ฉ๋ด : " + map.get(menu).name);
System.out.println("๊ฐ๊ฒฉ : " + map.get(menu).price);
System.out.println("๋ถ๋ฅ : " + map.get(menu).group);
System.out.println("์ค๋ช
: " + map.get(menu).desc);
System.out.println("--------------------------");
} else System.out.println("์ ํํ์ ๋ฉ๋ด๋ ์กด์ฌํ์ง ์์ต๋๋ค.");
break;
case 3 :
System.out.print("์ถ๊ฐํ ๋ฉ๋ด๋ฅผ ์
๋ ฅ ํ์ธ์ : ");
menu = sc.next();
if(map.containsKey(menu)) {
System.out.println("ํด๋น ๋ฉ๋ด๊ฐ ์ด๋ฏธ ์กด์ฌํฉ๋๋ค.");
}else {
System.out.print("๊ฐ๊ฒฉ : ");
int price = sc.nextInt();
System.out.print("๋ถ๋ฅ : ");
String group = sc.next();
sc.nextLine(); // ๋ฒํผ ๋น์ฐ๊ธฐ
System.out.print("์ค๋ช
: ");
String desc = sc.nextLine();
map.put(menu,new MenuInfo(menu, price, group, desc));
}
break;
case 4 :
System.out.print("์ญ์ ํ ๋ฉ๋ด๋ฅผ ์
๋ ฅ ํ์ธ์ : ");
menu = sc.next();
if(map.containsKey(menu)) {
map.remove(menu);
System.out.println(menu + "์(๋ฅผ) ์ญ์ ํ์์ต๋๋ค.");
}else System.out.println("์ญ์ ํ ๋ฉ๋ด๊ฐ ์์ต๋๋ค.");
break;
case 5 :
System.out.print("์์ ํ ๋ฉ๋ด๋ฅผ ์
๋ ฅ ํ์ธ์ ; ");
menu = sc.next();
if(map.containsKey(menu)) {
System.out.print("๋ฉ๋ด : ");
String name = sc.next();
System.out.print("๊ฐ๊ฒฉ : ");
int price = sc.nextInt();
System.out.print("๋ถ๋ฅ : ");
String group = sc.next();
sc.nextLine(); // ๋ฒํผ ๋น์ฐ๊ธฐ
System.out.print("์ค๋ช
: ");
String desc = sc.nextLine();
map.replace(menu,new MenuInfo(name, price, group, desc));
} else System.out.println("์์ ํ ๋ฉ๋ด๊ฐ ์์ต๋๋ค.");
break;
case 6 :
System.out.println("๋ฉ๋ด๋ฅผ ์ข
๋ฃ ํฉ๋๋ค.");
return;
}
}
}
}
HashTable
- HashMap ๋ด๋ถ ๊ตฌ์กฐ์ ๋์ผํ๋ฉฐ, ๋ฉํฐ ์ค๋ ๋๋ฅผ ์ง์ํฉ๋๋ค.
Properties
- ์ ๋ณด๋ฅผ "ํ์ผ"์ ์ ์ฅํ๊ฑฐ๋ ์ฝ๊ธฐ ์ํ ์ฉ๋๋ก ์ฌ์ฉํ๊ธฐ ์ํด key์ value ๋ชจ๋ ๋ฌธ์์ด๋ก๋ง ๊ตฌ์ฑ ํฉ๋๋ค.
- Properties๋ HashTable์ ์์ ๋ฐ์ ์ฌ์ฉ ํฉ๋๋ค.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException {
Properties properties = new Properties(); // HashTable ์์ ์์ ๋ฐ์ (K,V ๋ชจ๋ ๋ฌธ์์ด)
String path = Main.class.getResource("../ํ์์ ๋ณด์์ 0105/database.properties").getPath(); // ์๋๊ฒฝ๋ก(๋ด ์์น๋ฅผ ๊ธฐ์ค)
path = URLDecoder.decode(path, "utf-8"); // == ํ๊ธ์ด ํฌํจ ๋ ๊ฒฝ์ฐ ์ธ์์ ๋ชปํ๊ธฐ ๋๋ฌธ์ utf-8๋ก ๋์ฝ๋ฉํ๊ฒ ๋ค.
properties.load(new FileReader(path));
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println("driver : " + driver);
System.out.println("url : " + url);
System.out.println("username : " + username);
System.out.println("password : " + password);
}
}
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@local:1521:xe
username=scott
password=tiger