1. Map
키-값을 하나의 쌍으로 묶어서 저장하는 자료 구조. 맵은 중복된 키를 가질 수 없다.
Map<String,String> map = new HashMap<String,String>();
map.put("kim","1234"); // (키,값)
맵은 이렇게 값을 초기화할 수 있다.
Map<String, String> map = Map.of("kim","1234","lee","2345","han","3456"); // (키,값,키,값)
이렇게 한 줄로 값을 초기화 할 수도 있다.
public class MapTest {
public static void main(String[] args) {
Map<String, String> map = Map.of("kim", "1234", "lee", "2345", "han", "3456");
System.out.println(map);
System.out.println(map.get("lee"));
}
}
.get(키)값을 출력하면 해당 키의 값을 출력받을 수 있다.

2. Queue
큐는 데이터를 처리하기 전에 잠시 저장하고 있는 자료 구조. 선입선출 FIFO (first-in-first-out) 방식이다.
public class QueueTest {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < 5; i++) {
q.add(i);
}
System.out.println("큐의 요소" + q);
System.out.println("삭제된 요소:" + q.remove());
System.out.println(q);
System.out.println("삭제된 요소:" + q.remove());
System.out.println(q);
}
}
큐에 값을 0~5 까지 반복문을 통해 입력했다.
큐를 제거하면 입력된 순서대로 값이 삭제된다.

3. PriorityQueue (우선 순위 큐)
우선 순위 큐는 무작위로 삽입되어도 가장 작은 원소가 제거된다.
public class PriorityQueueTest {
public static void main(String[] args) {
PriorityQueue<Integer> p = new PriorityQueue<>();
p.add(100);
p.add(50);
p.add(30);
p.add(60);
System.out.println(p);
System.out.println("삭제된 값:" + p.remove());
System.out.println(p);
}
}

Share article