[자바 컬렉션과 Stream] 80. Queue

김건우's avatar
Feb 19, 2025
[자바 컬렉션과 Stream] 80. Queue
💡
데이터를 처리하기 전에 잠시 저장하고 있는 자료구조
FLFO (frist-in-frist-out) 형식으로 저장
예외적인 큐는 우선 순위 큐

예제 1. FLFO

package ex16; import java.util.LinkedList; import java.util.Queue; public class QueueTest { public static void main(String[] args) { Queue<Integer> q = new LinkedList<Integer>(); for (int i = 0; i < 5; i++) { q.add(i); } System.out.println("큐의 요소: " + q); int e = q.remove(); System.out.println("삭제된 요소: " + (e)); System.out.println(q); } }
notion image

예제 2. 우선 순위 큐

package ex16; // 우선순위 큐 // 원소들이 무작위로 삽입 되었더라도 정렬된 상태로 원소들을 추출 import java.util.PriorityQueue; public class PriorityQueueTest { public static void main(String[] args) { PriorityQueue<Integer> q = new PriorityQueue<Integer>(); q.add(30); q.add(80); q.add(20); System.out.println(q); System.out.println("삭제된 원소: " + q.remove()); // 작은 숫자가 우선 순위가 높다 } }
notion image
Share article

gunwoo