[Java] 浅谈Java中的Queue接口


Java中的Queue接口

本身很少用到这个接口,最近拿刷力扣时,用Java写bfs想着应该也和C++一样有着队列的接口,使了一下Queue果然有,但是它是一个接口,因此在网上查询了一下它的实现类,及相关用法。

Queue接口位于java.util包下,继承了Collection接口,用来存储满足FIFO(First in First out)原则的容器。

大体结构如下图:

image-20210125173622131

通常使用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()); 
    } 
} 

Queue的相关方法

image-20210125175623411

本文参考

Queue Interface In Java - GeeksforGeeks

Queue (Java Platform SE 8 ) (oracle.com)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM