Java中的Queue接口
本身很少用到這個接口,最近拿刷力扣時,用Java寫bfs想着應該也和C++一樣有着隊列的接口,使了一下Queue果然有,但是它是一個接口,因此在網上查詢了一下它的實現類,及相關用法。
Queue接口位於java.util包下,繼承了Collection接口,用來存儲滿足FIFO(First in First out)原則的容器。
大體結構如下圖:
通常使用PriorityQueue(優先隊列)和LinkedList來作為Queue的實現類,LinkedList我是沒有想到的畢竟命名不太一樣,但想到用鏈表來實現隊列會比數組更簡單也就釋然了。並且上述兩個容器都是線程不安全的,Java也提供了PriorityBlockingQueue作為線程安全的使用容器。
//declaration
public interface Queue extends Collection
創造Queue對象
因為Queue是接口,我們不能通過new Queue()創建。通常使用它的實現類創建,並且自從Java 1.5也需要在Queue中聲明泛型。
Queue<Obj> queue = new PriorityQueue<>();
例子:
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args)
{
Queue<Integer> q
= new LinkedList<>();
// 給q增加元素 {0, 1, 2, 3, 4}
for (int i = 0; i < 5; i++)
q.add(i);
// 顯示隊列
System.out.println("Elements of queue "
+ q);
// 移除隊列頭
int removedele = q.remove();
System.out.println("removed element-"
+ removedele);
System.out.println(q);
// 查看隊列頭
int head = q.peek();
System.out.println("head of queue-"
+ head);
// 也繼承了Collection接口的一些方法,如size()
int size = q.size();
System.out.println("Size of queue-"
+ size);
}
}
Queue的基本操作
添加元素
基本上也就是繼承了Collection的方法add(),隊列是添加在隊尾的。
import java.util.*;
public class GFG {
public static void main(String args[])
{
Queue<String> pq = new PriorityQueue<>();
pq.add("Geeks");
pq.add("For");
pq.add("Geeks");
System.out.println(pq);
}
}
刪除元素
有兩個函數可以用來刪除隊頭的元素,一個是remove()就是刪除隊頭元素,不會返回它;另外一個是poll()刪除隊頭元素並且返回它,個人來說算法題用poll()比較多。
import java.util.*;
public class GFG {
public static void main(String args[])
{
Queue<String> pq = new PriorityQueue<>();
pq.add("Geeks");
pq.add("For");
pq.add("Geeks");
System.out.println("Initial Queue " + pq);
pq.remove("Geeks");
System.out.println("After Remove " + pq);
System.out.println("Poll Method " + pq.poll());
System.out.println("Final Queue " + pq);
}
}
遍歷隊列
可以將隊列轉換為數組,通過索引遍歷;也可以通過for循環遍歷;當然也有迭代器的方式遍歷。
import java.util.*;
public class GFG {
public static void main(String args[])
{
Queue<String> pq = new PriorityQueue<>();
pq.add("Geeks");
pq.add("For");
pq.add("Geeks");
Iterator iterator = pq.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
}
}
Queue的實現類
1.PriorityQueue
優先隊列,C++中的優先隊列用的是桶排序的方法,不知道Java底層是如何實現的,這個實現類在構造哈夫曼樹這樣的結構非常好用,它實際上不能很好的滿足對於先入先入原則,因為畢竟經過排序。
import java.util.*;
class GfG {
public static void main(String args[])
{
// Creating empty priority queue
Queue<Integer> pQueue
= new PriorityQueue<Integer>();
// Adding items to the pQueue
// using add()
pQueue.add(10);
pQueue.add(20);
pQueue.add(15);
// Printing the top element of
// the PriorityQueue
System.out.println(pQueue.peek());
// Printing the top element and removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Printing the top element again
System.out.println(pQueue.peek());
}
}
2.LinkedList
這個類就是鏈表的實現,一種線性結構,但是不是存儲在連續的空間上,通過指針地址相互連接。具有非常好的插入刪除性能。
import java.util.*;
class GfG {
public static void main(String args[])
{
// Creating empty LinkedList
Queue<Integer> ll
= new LinkedList<Integer>();
// Adding items to the ll
// using add()
ll.add(10);
ll.add(20);
ll.add(15);
// Printing the top element of
// the LinkedList
System.out.println(ll.peek());
// Printing the top element and removing it
// from the LinkedList container
System.out.println(ll.poll());
// Printing the top element again
System.out.println(ll.peek());
}
}