Hash Map
package main;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class MainClass {
public static void main(String[] args) {
Map<Integer, String> hMap = new HashMap<Integer, String>();
hMap.put(101, "Lions");
hMap.put(102, "Tigers");
hMap.put(103, "Bears");
hMap.put(104, "Twins");
hMap.put(105, "Landers");
hMap.put(102, "Giants");
System.out.println(hMap.size());
Iterator<Integer> it = hMap.keySet().iterator();
while(it.hasNext()) {
Integer key = it.next();
String value = hMap.get(key);
System.out.println(key + ":" + value);
}
String deleteVal = hMap.remove(104);
System.out.println("삭제된 value:" + deleteVal);
String value = hMap.get(101);
boolean b = hMap.containsKey(102);
if(b == true) {
String val = hMap.get(102);
System.out.println("value:" + val);
}
hMap.replace(103, "Eagles");
it = hMap.keySet().iterator();
while(it.hasNext()) {
Integer key = it.next();
String val = hMap.get(key);
System.out.println(key + ":" + val);
}
String str = "Hello";
Integer in = 123;
ArrayList<String> list = new ArrayList<String>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("문자열", str);
map.put("숫자", in);
map.put("목록", list);
Map<String, String> fruit = new HashMap<String, String>();
fruit.put("apple", "사과");
fruit.put("plum", "자두");
fruit.put("banana", "바나나");
fruit.put("grape", "포도");
fruit.put("pear", "배");
Iterator<String> is = fruit.keySet().iterator();
while(is.hasNext()) {
String key = is.next();
String val = fruit.get(key);
System.out.println("key:" + key + "val:" + val);
}
fruit.remove("grape");
boolean b1 = fruit.containsKey("peach");
if (b1 == true) {
System.out.println("해당 키 값이 존재합니다..");
} else {
System.out.println("해당 키 값이 존재하지 않습니다.");
fruit.put("peach", "복숭아");
}
fruit.replace("pear", "나주배");
is = fruit.keySet().iterator();
while(is.hasNext()) {
String key = is.next();
String val = fruit.get(key);
System.out.println("key:" + key + "val:" + val);
}
TreeMap<String, String> tMap = new TreeMap<String, String>(fruit);
Iterator<String> it2 = tMap.descendingKeySet().iterator();
while(it2.hasNext()) {
String k = it2.next();
String v = tMap.get(k);
System.out.println("TreeMap key:" + k + "Value:" + v);
}
Iterator<String> it1 = fruit.keySet().iterator();
while(it1.hasNext()) {
String key = it1.next();
String v = fruit.get(key);
System.out.println("TreeMap key:" + key + "Value:" + v);
}
}
}