นี่แสดงถึงคอลเล็กชันที่เยื้องเพื่อเก็บข้อมูลก่อนประมวลผล เป็นการจัดเรียงประเภทเข้าก่อนออกก่อน (FIFO) องค์ประกอบแรกที่ใส่ในคิวคือองค์ประกอบแรกที่นำออกจากองค์ประกอบ
วิธีการแอบดู ()
เมธอดนี้ส่งคืนอ็อบเจ็กต์ที่ด้านบนสุดของคิวปัจจุบัน โดยไม่ต้องลบออก หากคิวว่างวิธีนี้จะคืนค่า null
ตัวอย่าง
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String args[]) {
Queue<String> queue = new LinkedList<String>();
queue.add("Java");
queue.add("JavaFX");
queue.add("OpenCV");
queue.add("Coffee Script");
queue.add("HBase");
System.out.println("Element at the top of the queue: "+queue.peek());
Iterator<String> it = queue.iterator();
System.out.println("Contents of the queue: ");
while(it.hasNext()) {
System.out.println(it.next());
}
}
} ผลลัพธ์
Element at the top of the queue: Java Contents of the queue: Java JavaFX OpenCV Coffee Script Hbase
วิธีการโพล()
แอบดู() วิธีการของ คิว อินเทอร์เฟซส่งคืนวัตถุที่ด้านบนของคิวปัจจุบันและลบออก หากคิวว่างวิธีนี้จะคืนค่า null
ตัวอย่าง
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String args[]) {
Queue<String> queue = new LinkedList<String>();
queue.add("Java");
queue.add("JavaFX");
queue.add("OpenCV");
queue.add("Coffee Script");
queue.add("HBase");
System.out.println("Element at the top of the queue: "+queue.poll());
Iterator<String> it = queue.iterator();
System.out.println("Contents of the queue: ");
while(it.hasNext()) {
System.out.println(it.next());
}
}
} ผลลัพธ์
Element at the top of the queue: Java Contents of the queue: JavaFX OpenCV Coffee Script HBase