package algo;
import java.util.HashMap;
import java.util.Map;
public class hashMap {
static class person{
int age;
String name;
public person(int age,String name){
this.age=age;
this.name=name;
}
}
public static void main(String[] args) {
Map<String, Integer> hm = new HashMap<>();
hm.put("둘리", 10);
hm.put("또치", 20);
hm.put("마이콜", 30);
//value존재 확인
System.out.println("10은 존재하는가? " + hm.containsValue(10));
System.out.println("50은 존재하는가? " + hm.containsValue(50));
//key존재 확인
boolean isExistsOne = hm.containsKey("둘리");
System.out.println("isExistsOne: " + isExistsOne);
for (String key: hm.keySet()){
System.out.println("{" + key + " => " + hm.get(key) + "}");
}
Map<Integer, person> hm2 = new HashMap<>();
hm2.put(1, new person(20,"a"));
hm2.put(2, new person(20,"b"));
hm2.put(3, new person(20,"c"));
System.out.println(hm2.get(2).age);
System.out.println(hm2.get(2).name);
}
}
댓글