큐
먼저 집어넣은 데이터가 먼저 나오는
FIFO(First In First Out) 구조
내용 출처 - 위키백과 -
수정자 및 타입 | 메서드 설명 |
bollean | boolean add(E e) 용량 제한을 위반하지 않는 다면 즉시 지정된 요소를 큐에 삽입하고, 성공 시 true를 반환 공간이 없는 경우 IgalidStateException 반환 Parameters : 추가할 요소 Returns : true Throws : IllegalStateException : 용량 제한으로 인해 요소를 추가할 수 없는 경우 ClassCastException : 지정된 요소의 클래스가 이 큐에 추가되지 않도록 하는 경우 NullPointerException : 지정한 요소가 null이고 이 큐에서 null 요소를 허용하지 않는 경우 IllegalArgumentException : 요소의 일부 속성으로 인해 큐에 추가할 수 없는 경우 |
boolean | boolean offer(E e) 용량 제한을 위반하지 않는 다면 즉시 지정된 요소를 이 큐에 삽입 용량 제한 큐를 사용할때, 예외처리를 해야만 실패한 삽입을 하는 add(E)보다 일반적으로 선호 Parameters : 추가할 요소 Returns : 요소가 이 큐에 추가되면 true, 그렇지 않으면 false Throws : ClassCastException : 지정된 요소의 클래스가 이 큐에 추가되지 않도록 하는 경우 NullPointerException : 지정된 요소가 null이고 이 큐에서 null 요소를 허용하지 않는 경우 IllegalArgumentException : 이 요소의 일부 속성으로 인해 이 큐에 추가할 수 없는 경우 |
E | E remove() 이 큐의 맨앞을 찾아 제거. 이 메서드는 이 큐가 비어 있는 경우 예외를 발생시킨다는 점에서만 poll()과 다름 Returns : 이 큐의 맨앞 Throws : NoSuchElementException : 이 큐가 비어 있는 경우 |
E | E poll() 이 큐의 맨 앞을 찾아 제거하거나, 이 큐가 비어 있으면 null을 반환 Returns : 이 큐의 맨앞 또는 이 큐가 비어 있는 경우 null |
E | E element() 이 큐의 맨앞을 검색하지만 제거는 하지 않는다. 이 메서드는 큐가 비어 있는 경우 예외를 발생 시킨다는 점에서만 peek와 다르다 Returns : 이 큐의 맨앞 Throws : NoSuchElementException : 이 큐가 비어 있는 경우 |
E | E peek() 이 큐의 맨앞을 검색하지만 제거하지는 않으며, 이 큐가 비어 있는 경우 null을 반환 Returns : 이 큐의 맨앞 또는 이 큐가 비어 있는 경우 null |
Queue (Java SE 15 & JDK 15)
Type Parameters: E - the type of elements held in this queue All Superinterfaces: Collection , Iterable All Known Subinterfaces: BlockingDeque , BlockingQueue , Deque , TransferQueue All Known Implementing Classes: AbstractQueue, ArrayBlockingQueue, ArrayD
docs.oracle.com
직접 한번 메서드를 사용해 보자
예제
add(E e), remove()
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
// 요소 추가
queue.add(10);
queue.add(20);
queue.add(30);
// 요소 출력
System.out.println("Queue: " + queue);
// 요소 삭제
int removed = queue.remove();
System.out.println("제거된 요소: " + removed);
// 요소 출력
System.out.println("제거후 Queue: " + queue);
}
}
-----------------------------------------------------------------
Queue: [10, 20, 30]
제거된 요소: 10
제거후 Queue: [20, 30]
Process finished with exit code 0
offer(E e), peek(), poll()
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
// 큐에 요소 추가
queue.offer(10);
queue.offer(20);
queue.offer(30);
System.out.println("queue = " + queue);
System.out.println();
// 큐에서 요소 가져오기
System.out.println("peek(): " + queue.peek()); // 10 출력
System.out.println("queue = " + queue);
System.out.println();
System.out.println("poll(): " + queue.poll()); // 10 출력
System.out.println("queue = " + queue);
System.out.println();
System.out.println("peek(): " + queue.peek()); // 20 출력
System.out.println("queue = " + queue);
}
}
--------------------------------------------------------------------
queue = [10, 20, 30]
peek(): 10
queue = [10, 20, 30]
poll(): 10
queue = [20, 30]
peek(): 20
queue = [20, 30]
Process finished with exit code 0
element()
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
// 큐에 요소 추가
queue.offer(10);
queue.offer(20);
queue.offer(30);
System.out.println("queue = " + queue);
System.out.println();
// 큐에서 요소 가져오기
System.out.println("element(): " + queue.element()); // 10 출력
System.out.println("queue = " + queue);
System.out.println();
System.out.println("poll(): " + queue.poll()); // 10 출력
System.out.println("queue = " + queue);
System.out.println();
System.out.println("element(): " + queue.element()); // 20 출력
System.out.println("queue = " + queue);
System.out.println();
}
}
--------------------------------------------------------------------------------
queue = [10, 20, 30]
element(): 10
queue = [10, 20, 30]
poll(): 10
queue = [20, 30]
element(): 20
queue = [20, 30]
Process finished with exit code 0
'자바 탐구' 카테고리의 다른 글
Intellij) 테마 바꾸기 (0) | 2023.04.03 |
---|---|
인터페이스) Comparator (0) | 2023.03.21 |
인터페이스) Comparable (0) | 2023.03.18 |
자료구조) 우선순위 큐 (0) | 2023.03.13 |
자료구조) 스택 (0) | 2023.03.12 |