대기열이란 무엇입니까?
이름에서 알 수 있듯이 Queue는 FIFO(First In First Out) 삽입 순서를 따르는 Java의 일반적인 데이터 구조입니다 . 식료품 점의 대기열로 쉽게 상상할 수 있습니다. 빨리 들어갈수록 빨리 나옵니다. 이는 이전에 대기열에 추가된 요소도 더 일찍 떠날 것임을 의미합니다. 대기열의 첫 번째 요소(앞)는 head 라고도 합니다 .Queue의 poll() 메서드는 무엇입니까?
poll() 메서드를 사용하면 Queue의 최상위 요소(헤드)를 검색하고 제거할 수 있습니다. 이 예에서 위의 대기열에서 poll()을 호출하면 결과로 '1'을 받게 됩니다. 그리고 큐에는 4개의 요소만 남게 됩니다.Java의 poll() 메소드
예 1
그림 1.0에 표시된 대기열에서 poll() 함수 를 호출하는 간단한 예를 살펴보십시오 .
import java.util.LinkedList;
import java.util.Queue;
public class QueuePollMethod {
public static void main(String[] args) {
// create a queue of die rolls
Queue dieRoll = new LinkedList();
// Add 6 integers one by one
dieRoll.add(1);
dieRoll.add(2);
dieRoll.add(3);
dieRoll.add(4);
dieRoll.add(5);
dieRoll.add(6);
// print the original queue
System.out.println("Queue:\t" + dieRoll + "\n");
// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
}
}
산출
대기열: [1, 2, 3, 4, 5, 6] poll() 반환됨: 1 대기열 업데이트됨! [2, 3, 4, 5, 6] poll() 반환: 2 대기열 업데이트! [3, 4, 5, 6] poll() 반환: 3 대기열 업데이트! [4, 5, 6]
각 poll() 호출 후 대기열의 크기가 1씩 줄어들고 헤드가 반환되는 방식에 유의하십시오 .
예 2
일주일의 다른 예를 살펴보겠습니다. 우리는 평일에 하나씩 poll()을 호출 하고 어떻게 보이는지 볼 것입니다.
import java.util.LinkedList;
import java.util.Queue;
public class QueuePollMethod {
public static void main(String[] args) {
Queue days = new LinkedList();
days.add("Sunday");
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
// print all the days in the week
System.out.println("Week Days: \t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
}
}
산출
요일: [일요일, 월요일, 화요일, 수요일, 목요일, 금요일, 토요일] poll() 반환: 일요일 업데이트! [월요일, 화요일, 수요일, 목요일, 금요일, 토요일] poll() 반환: 월요일 업데이트! [화요일, 수요일, 목요일, 금요일, 토요일] poll() 반환됨: 화요일 업데이트됨! [수,목,금,토]
poll()은 peek() 및 remove()와 어떻게 다른가요?
세 가지 개념이 매우 비슷해 보이기 때문에 초보자도 쉽게 혼합할 수 있습니다. "q"를 대기열로 합시다. 그러면 서로 다른 점은 다음과 같습니다.- q.poll() : 대기열의 헤드를 제거하고 검색합니다.
- q.peek() : 제거하지 않고 대기열의 헤드만 검색/반환합니다.
- q.remove() : 대기열의 헤드를 제거하고 검색합니다.
예 3
이제 위의 세 가지 기능에 대한 몇 가지 예를 간단히 살펴보겠습니다.
import java.util.LinkedList;
import java.util.Queue;
public class QueuePollMethod {
public static void main(String[] args) {
Queue days = new LinkedList();
days.add("Sunday");
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
// print all the days in the week
System.out.println("Week Days: \t" + days + "\n");
// after calling peek()
System.out.println("peek() returned: " + days.peek());
System.out.println("Week Days: \t" + days + "\n");
System.out.println("peek() returned: " + days.peek());
System.out.println("Week Days: \t" + days + "\n");
// after calling remove()
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");
// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
System.out.println("poll() returned: " + days.poll());
System.out.println("remove() returned: " + days.remove());
}
}
산출
요일: [일요일, 월요일, 화요일, 수요일, 목요일, 금요일, 토요일] peek() 반환: 일요일 요일: [일요일, 월요일, 화요일, 수요일, 목요일, 금요일, 토요일] peek() 반환: 일요일 주중 : [일요일, 월요일, 화요일, 수요일, 목요일, 금요일, 토요일] remove() 반환: 일요일 업데이트! [월요일, 화요일, 수요일, 목요일, 금요일, 토요일] remove() 반환: 월요일 업데이트! [화요일, 수요일, 목요일, 금요일, 토요일] remove() 반환됨: 화요일 remove() 반환됨: 수요일 remove() 반환됨: 목요일 remove() 반환됨: 금요일 업데이트됨! [토요일] poll() 반환: 토요일 업데이트됨! [] poll()이 반환됨: java.util.LinkedList.removeFirst(LinkedList.java:270)의 "main" 스레드 java.util.NoSuchElementException에서 null 예외 java.util.LinkedList.remove(LinkedList.java:
peek() 호출 후 볼 수 있듯이 평일은 동일하게 유지됩니다. 그리고 remove() 또는 poll()을 사용하면 대기열의 크기가 헤드 반환과 함께 1씩 줄어듭니다. 또한 빈 대기열에서 remove()를 호출하면 java.util.NoSuchElementException 이 발생 하지만 poll()을 사용하면 예외가 발생하지 않습니다 .
GO TO FULL VERSION