Struktur data baris gilir menggunakan prinsip First In First Out (FIFO). Ia digunakan untuk memegang objek yang akan diproses mengikut urutan kedatangan mereka; ini hampir sama dengan barisan orang yang berdiri dalam barisan. Memandangkan Java menyediakan sokongan besar untuk struktur data dalam bentuk antara muka Koleksi, baris gilir ialah antara muka yang tersedia dalam antara muka Koleksi. Ia memanjangkan antara muka Koleksi. Ia tersedia dalam pakej Java.util dan menyokong semua operasi yang tersedia dalam antara muka Koleksi, bersama-sama dengan beberapa operasi pengekstrakan, sisipan dan pemeriksaan tambahan.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Sintaks:
Interface Queue<E>
Baris gilir ialah antara muka, bukan kelas, jadi ia tidak boleh langsung. Pengisytiharan menunjukkan bahawa baris gilir menerima nilai sebagai generik yang serupa dengan koleksi, dan kami boleh menghantar sebarang objek kepadanya. Java mempunyai pelbagai pelaksanaan antara muka Queue, yang boleh kita gunakan semasa menggunakan Queues. Ia adalah LinkedList dan PriorityQueue.
Baris gilir boleh diisytiharkan seperti di bawah:
Queue< Object > q = new LinkedList<>();
Queue< Object > q = new PriorityQueue<>();
Jenis Ahli Gilir di Jawa
Di bawah adalah semua kaedah yang tersedia dalam baris gilir:
Returns special value | Throws exception | |
Insert | offer(e) | add(e) |
Remove | poll() | remove() |
Examine | peek() | element() |
So as explained, two types of methods throw an exception and return a special value. There are three types of operation in this kind of operation: insertion, the second is removal, and the third is retrieval or examination. In the case of the remove operation, an object will be removed from the queue. Still, in the case of examination, the object will be returned without actually removing from the queue.
Given below are the different examples of Queue in Java:
Code:
import java.util.LinkedList; import java.util.Queue; public class QueueOperations { public static void main(String[] args) { Queue<Integer> q = new LinkedList<Integer> (); q.add(5); q.add(2); q.add(1); q.add(4); q.add(3); System.out.println(q); } }
Output:
Note here that the order of insertion is the same with output from left to write.
Code:
import java.util.LinkedList; import java.util.Queue; public class QueueOperations { public static void main(String[] args) { Queue<Integer> q = new LinkedList<Integer> (); q.add(5); q.add(2); q.add(1); q.add(4); q.add(3); System.out.println(q); while (!q.isEmpty()) { System.out.print(q.remove() + " "); } System.out.println(""); System.out.println(q); } }
Output:
Here, we have used the function isEmpty() to check when the queue becomes empty after removing elements. The removal order is the same as per the insertion. After removing all the elements, we printed the queue and obtained an empty bracket at the end.
Code:
import java.util.PriorityQueue; import java.util.Queue; public class QueueOperations { public static void main(String[] args) { Queue<Integer> q = new PriorityQueue<Integer> (); q.add(5); q.add(2); q.add(1); q.add(4); q.add(3); System.out.println(q); while (!q.isEmpty()) { System.out.print(q.remove() + " "); } System.out.println(""); System.out.println(q); } }
Output:
Here, we have used PriorityQueue, which will hold and return the elements depending upon the elements’ natural ordering or upon the comparator, if any passed. Note the insertion order and removal orders are not the same. The removal is based totally on the value of elements.
Code:
import java.util.LinkedList; import java.util.Queue; public class QueueOperations { public static void main(String[] args) { Queue<Integer> q = new LinkedList<Integer> (); q.add(5); q.add(2); q.add(1); q.add(4); q.add(3); System.out.println(q); System.out.println( q.peek() ); System.out.println(q); } }
Output:
Note here that we have used the peek() function, which will return the head of the queue without actually removing it. We printed the queue after performing the peek operation, and you can observe that the head element, which is 5, remains unchanged in the queue.
Code:
import java.util.PriorityQueue; import java.util.Queue; public class QueueOperations { public static void main(String[] args) { Queue<Integer> q = new PriorityQueue<Integer> (); q.add(5); q.add(2); q.add(1); q.add(4); q.add(3); System.out.println(q); System.out.println( q.peek() ); System.out.println(q); } }
Output:
This is similar to the previous example’s LinkedList operation, but note the head element is 1 because it’s a PriorityQueue.
Java utilizes the Queue interface as a means to maintain elements in insertion order. It supports operations like insertion, retrieval, and removal. There are alternative methods available for all the methods. We have seen examples of the most commonly used methods in queue operation.
Atas ialah kandungan terperinci Beratur di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!