HashMap<String,String> map1 = new HashMap<String,String>();//HashMap생성
HashMap<String,String> map2 = new HashMap<>();//new에서 타입 파라미터 생략가능
HashMap<String,String> map3 = new HashMap<>(map1);//map1의 모든 값을 가진 HashMap생성
HashMap<String,String> map4 = new HashMap<>(10);//초기 용량(capacity)지정
HashMap<String,String> map5 = new HashMap<>(10, 0.7f);//초기 capacity,load factor지정
HashMap<String,String> map6 = new HashMap<String,String>(){{//초기값 지정
put("a","b");
}};
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
fruits.put(null, 4);
fruits.put("kiwi", 5);
System.out.println("fruits: " + fruits);
key : 값을 가져와야 하는 요소의 키
defaultValue : 지정된 키로 매핑된 값이 없는 경우 반환
fruits.get("키값")
Collection<Integer> values = fruits.values();
for (Entry<Integer, String> entry : map.entrySet()) {
System.out.println("[Key]:" + entry.getKey() + "
}
Set<String> keys = fruits.keySet();
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("remove(apple): " + fruits.remove("apple"));
System.out.println("remove(kiwi): " + fruits.remove("kiwi"));
System.out.println("remove(undefined): " + fruits.remove("undefined"));
System.out.println("fruits: " + fruits);
System.out.println("containsKey(apple): " + fruits.containsKey("apple"));
System.out.println("containsKey(undefined): " + fruits.containsKey("undefined"));
System.out.println("containsValue(1): " + fruits.containsValue(1));
System.out.println("containsValue(0): " + fruits.containsValue(0));
System.out.println("replace(apple, 10): " + fruits.replace("apple", 10));
System.out.println("replace(undefined, 10): " + fruits.replace("undefined", 10));
System.out.println("fruits: " + fruits);