标签:pq Java 浅谈 System Queue add println out
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());
}
}
Queue的相关方法
本文参考
标签:pq,Java,浅谈,System,Queue,add,println,out 来源: https://www.cnblogs.com/coderhu1/p/14326520.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。