내 맘대로 알고리즘

Java Queue, Priority Queue 예제


-1. 

이전글


2018/12/31 - [내 맘대로 알고리즘] - Java ArrayList, Vector 예제



0. 

Queue(큐)

Java에서 제공하고 있는 Queue는 인터페이스 형태로 LinkedList를 통해서 생성합니다.

그렇기 때문에 사이즈가 가변적이고, 쉽게 늘어난 다는 것이 특징입니다.


또한 Queue의 중요한 특징FIFO(First In First Out)으로

먼저 들어온 데이터가 먼저 출력되는 자료구조로 쓰는 것이 가장 큰 특징입니다.


예를 들면.


A<B<C

ABC순으로 데이터가 들어온다면 poll을 할 경우

ABC가 출력된다는 것이 가장 큰 특징이다.



1-0. 개념 설명


해당 코드에서는 5,1,2,3,4의 순서로 데이터를 집어 넣을 것이고,

데이터를 출력하고, 데이터를 확인하는 과정을 로직으로 설명합니다.


1-1. 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.LinkedList;
import java.util.Queue;
 
public class ExampleQueue {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();//큐 생성
        queue.offer(5);//데이터를 집어 넣음
        queue.offer(1);//데이터를 집어 넣음
        queue.offer(2);//데이터를 집어 넣음
        queue.offer(3);//데이터를 집어 넣음
        queue.offer(4);//데이터를 집어 넣음
        print(queue);//큐를 출력
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("peek: %d", queue.peek()));//peek
        System.out.println(String.format("peek: %d", queue.peek()));//peek
        
    }
    
    public static void print(Queue<Integer> queue) {
        System.out.print("data: ");
        queue.forEach((value)->{
            System.out.print(value);
        });
        System.out.println(String.format(" size: %d", queue.size())); 
    }
}
cs


1-2. 매소드


1
2
3
4
Queue<Integer> queue = new LinkedList<>(); //Queue인터페이스는 LinkedList를 통해서 생성합니다.
queue.offer(data); queue에 데이터를 집어 넣습니다.
queue.poll(); //queue에 있는 맨 앞에 있는 데이터를 뽑습니다.
queue.peek(); //queue의 맨 앞의 데이터를 확인합니다.
cs

1-3. 결과값


 



2.

Priority Queue(우선순위 큐)


Priority Queue의 특징은 2가지 입니다.

- 가장 가중치가 낮은 순서로 poll, peek()을 할 수 있는 자료구조입니다.

- Min Heap으로 데이터를 sort 시켜놓고 데이터를 출력하는 자료구조입니다.


따라서, 해당 자료구조를 통해서 데이터를 집어 넣게 되면

가중치에 따라서 데이터를 사용해야 하는 경우에 편하게 사용할 수 있는

것이 가장 큰 특징이라고 할 수 있습니다.




3-0. 개념 설명


해당 코드는 5,1,2,3,4 순으로 데이터를 넣지만 Min Heap으로 데이터를 정렬시켜 놓기 때문에 13254를 출력하는 것을 볼 수 있습니다.

또한 poll이나 peek을 할 경우에 자료구조의 가장 작은 데이터를 출력할 수 있는 것을 확인할 수 있습니다.


3-1. 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.PriorityQueue;
import java.util.Queue;
 
public class ExamplePriorityQueue {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();//큐 생성
        queue.offer(5);//데이터를 집어 넣음
        queue.offer(1);//데이터를 집어 넣음
        queue.offer(2);//데이터를 집어 넣음 
        
        queue.offer(3);//데이터를 집어 넣음
        queue.offer(4);//데이터를 집어 넣음
        print(queue);//큐를 MIN HEAP으로 SORT해서 출력
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        System.out.println(String.format("poll: %d", queue.poll()));//poll
        
    }
    
    public static void print(Queue<Integer> queue) {
        System.out.print("data: ");
        queue.forEach((value)->{
            System.out.print(value);
        });
        System.out.println(String.format(" size: %d", queue.size())); 
    }
}
cs

3-2. 매소드


1
2
3
4
PriorityQueue<Integer> queue = new PriorityQueue<>(); //PriorityQueue를  생성합니다.
queue.offer(data); queue에 데이터를 집어 넣습니다.
queue.poll(); //queue에 가장 작은 데이터를 뽑습니다.
queue.peek(); //queue의 가장 작은 데이터를 확인합니다.
cs


3-3.  결과값