본문으로 건너뛰기

Map기본연습

기본연습

맵에 값을 넣기
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> employees = new HashMap<Integer, String>();

employees.put(Integer.valueOf(1), "James");
employees.put(Integer.valueOf(2), "David");
employees.put(Integer.valueOf(3), "Cavin");
employees.put(Integer.valueOf(4), "Lucy");
employees.put(Integer.valueOf(5), "Tom");

System.out.println(employees);
}
}
{1=James, 2=David, 3=Cavin, 4=Lucy, 5=Tom}

두개의 맵을 합치기
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruit1 = new HashMap<Integer, String>();

fruit1.put(Integer.valueOf(1), "Apple");
fruit1.put(Integer.valueOf(2), "Kiwi");
fruit1.put(Integer.valueOf(3), "Banana");
fruit1.put(Integer.valueOf(4), "Melon");
fruit1.put(Integer.valueOf(5), "Orange");

System.out.println(fruit1);

Map<Integer, String> fruit2 = new HashMap<Integer, String>();

fruit2.put(Integer.valueOf(1), "Strawberry");
fruit2.put(Integer.valueOf(2), "Raspberry");
fruit2.put(Integer.valueOf(10), "Watermelon");
fruit2.put(Integer.valueOf(15), "Peach");

System.out.println(fruit2);

fruit1.putAll(fruit2);

System.out.println(fruit1);
}
}
{1=Apple, 2=Kiwi, 3=Banana, 4=Melon, 5=Orange}
{1=Strawberry, 2=Raspberry, 10=Watermelon, 15=Peach}
{1=Strawberry, 2=Raspberry, 3=Banana, 4=Melon, 5=Orange, 10=Watermelon, 15=Peach}

두개의 맵을 합칠경우에 중복되는 키값이 있다면 덮어쓰기가 된다.


키로 벨류 값을 추출
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruits = new HashMap<Integer, String>();

fruits.put(Integer.valueOf(1), "Strawberry");
fruits.put(Integer.valueOf(2), "Raspberry");
fruits.put(Integer.valueOf(10), "Watermelon");
fruits.put(Integer.valueOf(15), "Peach");

System.out.println(fruits.get(Integer.valueOf(1)));
System.out.println(fruits.get(Integer.valueOf(2)));
System.out.println(fruits.get(Integer.valueOf(3)));
System.out.println(fruits.get(Integer.valueOf(10)));
}
}
Strawberry
Raspberry
null
Watermelon

비어있을때는 null을 반환한다


키값으로 맵핑을 지울때
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruits = new HashMap<Integer, String>();

fruits.put(Integer.valueOf(1), "Strawberry");
fruits.put(Integer.valueOf(2), "Raspberry");
fruits.put(Integer.valueOf(10), "Watermelon");
fruits.put(Integer.valueOf(15), "Peach");

System.out.println(fruits);

// Delete
fruits.remove(Integer.valueOf(1));

System.out.println(fruits);
}
}
{1=Strawberry, 2=Raspberry, 10=Watermelon, 15=Peach}
{2=Raspberry, 10=Watermelon, 15=Peach}

모든 맵핑을 지울 때
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruits = new HashMap<Integer, String>();

fruits.put(Integer.valueOf(1), "Strawberry");
fruits.put(Integer.valueOf(2), "Raspberry");
fruits.put(Integer.valueOf(10), "Watermelon");
fruits.put(Integer.valueOf(15), "Peach");

System.out.println(fruits);

// Delete all
fruits.clear();

System.out.println(fruits);
}
}
{1=Strawberry, 2=Raspberry, 10=Watermelon, 15=Peach}
{}

맵핑의 갯수를 알아보기
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruits = new HashMap<Integer, String>();

fruits.put(Integer.valueOf(1), "Strawberry");
fruits.put(Integer.valueOf(2), "Raspberry");
fruits.put(Integer.valueOf(10), "Watermelon");
fruits.put(Integer.valueOf(15), "Peach");

System.out.println(fruits.size());
}
}
4

루프를 돌며 맵의 내용을 출력
import java.util.HashMap;
import java.util.Map;

public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruits = new HashMap<Integer, String>();

fruits.put(Integer.valueOf(1), "Strawberry");
fruits.put(Integer.valueOf(2), "Raspberry");
fruits.put(Integer.valueOf(10), "Watermelon");
fruits.put(Integer.valueOf(15), "Peach");

// 루프를 돌며 맵의 키값을 출력
for(Integer key : fruits.keySet()) {
System.out.println("key : " + key);
}

// 루프를 돌며 맵의 벨류 값을 출력
for(String value : fruits.values()) {
System.out.println("value : " + value);
}

// 루프를 돌며 맵의 키와 벨류 값을 출력
for(Map.Entry<Integer, String> entry : fruits.entrySet()) {
System.out.println(
"key : " + entry.getKey()
+ " , value : " + entry.getValue());
}
}
}
key : 1
key : 2
key : 10
key : 15
value : Strawberry
value : Raspberry
value : Watermelon
value : Peach
key : 1 , value : Strawberry
key : 2 , value : Raspberry
key : 10 , value : Watermelon
key : 15 , value : Peach

벨류 값 뽑는것을 제일 많이 본것 같은데, 마지막것도 가끔은 쓰이니 숙지할 것. Entry와 entrySet() !!


읽기전용 맵을 만들기
public class Exercise1 {

public static void main(String[] args) {

Map<Integer, String> fruits = new HashMap<Integer, String>();

fruits.put(Integer.valueOf(1), "Strawberry");
fruits.put(Integer.valueOf(2), "Raspberry");
fruits.put(Integer.valueOf(10), "Watermelon");
fruits.put(Integer.valueOf(15), "Peach");

// 읽기전용 맵으로 한다
Map<Integer, String> readOnlyFruits = Collections.unmodifiableMap(fruits);

for(String fruitssList : readOnlyFruits.values()) {
System.out.println(fruitssList);
}

// 맵에 풋을 하면 예외가 발생함
readOnlyFruits.put(Integer.valueOf(5), "Orange");
}
}

경우에 따라서 맵을 읽기전용으로 만들어야 할 때 사용하면 된다.
import java.util.Collection; 이 아닌
import java.util.stream.Collector; 도 아닌
import java.util.Collections; 이니 햇깔려하지 말것

Strawberry
Raspberry
Watermelon
Peach
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.put(Collections.java:1459)
at MapExercise.Exercise1.main(Exercise1.java:28)