「java队列分类」java定义队列
本篇文章给大家谈谈java队列分类,以及java定义队列对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java中的queue类有哪些用法?
java中的queue类是队列数据结构管理类。在它里边的元素可以按照添加它们的相同顺序被移除。
队列通常(但并非一定)以 FIFO(先进先出)的方式排序各个元素。不过优先级队列和 LIFO 队列(或堆栈)例外,前者根据提供的比较器或元素的自然顺序对元素进行排序,后者按 LIFO(后进先出)的方式对元素进行排序。无论使用哪种排序方式,队列的头都是调用remove()或poll()所移除的元素。在 FIFO 队列中,所有的新元素都插入队列的末尾。其他种类的队列可能使用不同的元素放置规则。每个Queue实现必须指定其顺序属性。
offer 添加一个元素并返回true 如果队列已满,则返回false
poll 移除并返问队列头部的元素 如果队列为空,则返回null
peek 返回队列头部的元素 如果队列为空,则返回null
put 添加一个元素 如果队列满,则阻塞
take 移除并返回队列头部的元素 如果队列为空,则阻塞
element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素 如果队列为空,则抛出一个
NoSuchElementException异常
注意:poll和peek方法出错进返回null。因此,向队列中插入null值是不合法的。
还有带超时的offer和poll方法重载,例如,下面的调用:
boolean success = q.offer(x,100,TimeUnit.MILLISECONDS);
尝试在100毫秒内向队列尾部插入一个元素。如果成功,立即返回true;否则,当到达超时进,返回false。同样地,调用:
Object head = q.poll(100, TimeUnit.MILLISECONDS);
如果在100毫秒内成功地移除了队列头元素,则立即返回头元素;否则在到达超时时,返回null。
阻塞操作有put和take。put方法在队列满时阻塞,take方法在队列空时阻塞。
Queue接口与List、Set同一级别,都是继承了Collection接口。LinkedList实现了Queue接 口。Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类型如果是Queue时,就完全只能访问Queue接口所定义的方法 了,而不能直接访问 LinkedList的非Queue的方法),以使得只有恰当的方法才可以使用。BlockingQueue 继承了Queue接口。
java 队列
//通过LinkedList实现队列
package 队列和堆栈;
import java.util.*;
public class LinkedListQueueTest {
//字段
private LinkedList list;
//无参数构造
public LinkedListQueueTest()
{
list=new LinkedList();
}
//队列元素的个数
public int size()
{
return list.size();
}
//进入队列
public void enqueue(Object obj)
{
list.addLast(obj);
}
//对头出来
public Object dequeue()
{
return list.removeFirst();
}
//浏览对头元素
public Object front()
{
//return list.getFirst();
return list.peekFirst();
}
//判断队列是否为空
public boolean isEmpty()
{
return list.isEmpty();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedListQueueTest llq=new LinkedListQueueTest();
System.out.println(llq.isEmpty());
llq.enqueue("147");
llq.enqueue("258");
llq.enqueue("369");
System.out.println(llq.size());
System.out.println("移除队列头元素:"+llq.dequeue());
System.out.println(llq.size());
llq.enqueue("abc");
llq.enqueue("def");
System.out.println(llq.size());
System.out.println("查看队列的头元素:"+llq.front());
System.out.println(llq.size());
System.out.println(llq.isEmpty());
}
}
通过数组实现
package 队列和堆栈;
import java.util.NoSuchElementException;
//通过数组来实现队列
public class ArrayQueue {
//字段
public static Object[] data;
//队列的元素个数
protected int size ;
//队列头
protected int head;
//队列尾
public static int tail;
/**
*
*/
//无参数构造函数
public ArrayQueue() {
final int INITIAL_LENGTH=3;
data=new Object[INITIAL_LENGTH];
size=0;
head=0;
tail=-1;
}
//队列元素个数方法
public int size()
{
return size;
}
public boolean isEmpty()
{
return size==0;
}
//得到队列头元素
public Object front()
{
if(size==0)
throw new NoSuchElementException();
return data[head];
}
//进入队列enqueue()方法
public void enqueue(Object obj)
{
//此时队列已经满
if(size==data.length){
Object[] oldData=data;
data=new Object[data.length*2];
//if(head==0)
System.arraycopy(oldData, head, data, 0, oldData.length-head);
if(head0)
System.arraycopy(oldData, 0, data, head+1, tail+1);
head=0;
tail=oldData.length-1;
}
tail=(tail+1)%data.length;
size++;
data[tail]=obj;
}
//队列的元素出队
public Object dequeue()
{
if(size==0)
throw new NoSuchElementException();
Object ele=data[head];
//循环队列
head=(head+1)%data.length;
size--;
return ele;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
通过向量实现:
//通过向量实现栈
package 队列和堆栈;
import java.util.*;
public class VectorStackTest {
//字段
Vector v;
//构造函数
public VectorStackTest()
{
v=new Vector();
}
//元素的个数
public int size()
{
return v.size();
}
//是否为空
public boolean isEmpty()
{
return size()==0;
}
//进栈
public Object Push(Object obj)
{
v.addElement(obj);
return obj;
}
//出栈方法
public Object Pop()
{
int len=size();
Object obj=Peek();
v.removeElementAt(len-1);
return obj;
}
//查看栈顶元素
public Object Peek()
{
int len = size();
if (len == 0)
throw new EmptyStackException();
return v.elementAt(len - 1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
VectorStackTest vst=new VectorStackTest();
System.out.println("大小:"+vst.size());
vst.Push("123");
vst.Push("456");
vst.Push("789");
vst.Push("abc");
System.out.println("大小:"+vst.size());
System.out.println("栈顶:"+vst.Peek());
System.out.println("出栈:"+vst.Pop());
vst.Push("def");
vst.Push("147");
System.out.println("大小:"+vst.size());
System.out.println("栈顶:"+vst.Peek());
System.out.println("出栈:"+vst.Pop());
System.out.println(vst.Peek());
vst.Push("def");
vst.Push("147");
System.out.println(vst.Pop());
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println(vst.Pop());
System.out.println(vst.Pop());
vst.Push("1aadf");
vst.Push("2dafad");
vst.Push("123789");
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println("------------------end------------");
VectorStackTest llst=new VectorStackTest();
llst.Push("123");
llst.Push("456");
System.out.println("栈顶:"+llst.Peek());
System.out.println("出栈:"+llst.Pop());
System.out.println(llst.Peek());
llst.Push("789");
llst.Push("abc");
System.out.println("栈顶:"+llst.Peek());
System.out.println("出栈:"+llst.Pop());
System.out.println(llst.size());
System.out.println("栈顶:"+llst.Peek());
}
}
推荐:都看API文档。有疑问可以问我,QQ:285479197
到底什么是消息队列?Java中如何实现消息队列
“消息队列”是在消息的传输过程中保存消息的容器。和我们学过的LinkedHashMap,TreeSet等一样,都是容器。既然是容器,就有有自己的特性,就像LinkedHashMap是以键值对存储。存取顺序不变。而消息队列,看到队列就可以知道。这个容器里面的消息是站好队的,一般遵从先进先出原则。
java中已经为我们封装好了很多的消息队列。在java 1.5版本时推出的java.util.concurrent中有很多现成的队列供我们使用。特性繁多,种类齐全。是你居家旅游开发必备QAQ。
下面简单列举这个包中的消息队列
:阻塞队列 BlockingQueue
数组阻塞队列 ArrayBlockingQueue
延迟队列 DelayQueue
链阻塞队列 LinkedBlockingQueue
具有优先级的阻塞队列 PriorityBlockingQueue
同步队列 SynchronousQueue
阻塞双端队列 BlockingDeque
链阻塞双端队列 LinkedBlockingDeque
不同的队列不同的特性决定了队列使用的时机,感兴趣的话你可以详细了解。具体的使用方式我就不赘述了
java中的队列都有哪些,有什么区别
阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞。试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其他的线程往空的队列插入新的元素。同样,试图往已满的阻塞队列中添加新元素的线程同样也会被阻塞,直到其他的线程使队列重新变得空闲起来,如从队列中移除一个或者多个元素,或者完全清空队列.
从5.0开始,JDK在java.util.concurrent包里提供了阻塞队列的官方实现。尽管JDK中已经包含了阻塞队列的官方实现,但是熟悉其背后的原理还是很有帮助的。一下是阻塞队列的实现:
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
JAVA中队列和栈的区别
队列(Queue):是限定只能在表的一端进行插入和在另一端进行删除操作的线性表;
栈(Stack):是限定只能在表的一端进行插入和删除操作的线性表。
区别如下:
一、规则不同
1. 队列:先进先出(First In First Out)FIFO
2. 栈:先进后出(First In Last Out )FILO
二、对插入和删除操作的限定不同
1. 队列:只能在表的一端进行插入,并在表的另一端进行删除;
2. 栈:只能在表的一端插入和删除。
三、遍历数据速度不同
1.
队列:基于地址指针进行遍历,而且可以从头部或者尾部进行遍历,但不能同时遍历,无需开辟空间,因为在遍历的过程中不影响数据结构,所以遍历速度要快;
2.
栈:只能从顶部取数据,也就是说最先进入栈底的,需要遍历整个栈才能取出来,而且在遍历数据的同时需要为数据开辟临时空间,保持数据在遍历前的一致性。
java队列分类的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java定义队列、java队列分类的信息别忘了在本站进行查找喔。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。