一、问题
线程Queue中有哪些方法?需要注意什么?
二、分析
1.队列是为了存储优先执行顺序而设计的集合;除此之外,基本的操作,队列提供额外的插入,取出,检查操作。任何方法以2种形式存在:1.如果操作失败,抛出异常;2.返回特殊值(如null,false);后者是为了插入操作容量限制而特别设计的实现类;在大多数实现类中,插入操作不允许失败;
2.队列基本上,但是非必须,以先进先出的方式(FIFO (first-in-first-out) )进出;异常中间是优先队列,这
java.util包中的Queue类中,有明确说明;
3、具体方法
①、add()方法
如果容量允许,则插入特定的元素到当前队列中,如果插入成功,则返回true,否则返回false;如果空间不够,则会抛出异常IllegalStateException;
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning
* {@code true} upon success and throwing an {@code IllegalStateException}
* if no space is currently available.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean add(E e);
②、offer()方法
如果容量允许,则插入特定的元素到当前队列中;如果使用的是容量限制的队列,该方法会比add()方法更好,因为add()插入失败时仅仅会抛出一个异常;
所以,offer()和add()最大的区别是:add仅仅会抛异常,而offer会返回false;
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* When using a capacity-restricted queue, this method is generally
* preferable to {@link #add}, which can fail to insert an element only
* by throwing an exception.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean offer(E e);
③、remove()方法
扫描并移除队列中头部的对象;
/**
* Retrieves and removes the head of this queue. This method differs
* from {@link #poll poll} only in that it throws an exception if this
* queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
E remove();
④、poll()方法
移除队列头部的元素;如果队列为空,则返回null;这个方法和remove()方法只有一个不同,那就是,如果队列为空,remove方法会报错,二poll()方法会返回null;
/**
* Retrieves and removes the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E poll();
⑤、Element()方法
检索但是不会移除队列头部的元素,这个方法不同于peek()方法,因为如果队列为空,它仅仅会抛出异常,而不是返回null;
/**
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
⑥、peek()方法
检索,但是不会移除队列头部的元素,如果队列为空,则返回null;
/**
- Retrieves, but does not remove, the head of this queue,
- or returns {@code null} if this queue is empty.
- @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
4、总结
5、测试结果
①、正常使用的队列:
public class TestQueue {
public static void main(String[] args) {
try {
Queue<String> queue = new ArrayBlockingQueue<String>(100);
//添加元素:一般原则,先进先出,后进后出;可自定义;
queue.add("1");
queue.offer("2");
//获取队列头部的第一个元素
String element = queue.element();
System.out.println("通过element()方法获取元素:"+element);
//获取队列头部的第一个元素
String peek = queue.peek();
System.out.println("通过peek()方法获取元素:"+peek);
System.out.println("现在的队列中元素为:"+ JSONObject.toJSONString(queue));
//移除队列头部第一个元素
String remove = queue.remove();
System.out.println("remove移除的结果为:"+ JSONObject.toJSONString(remove));
System.out.println("现在的队列中元素为:"+ JSONObject.toJSONString(queue));
//移除队列头部第一个元素
String poll = queue.poll();
System.out.println("poll移除的结果:"+ JSONObject.toJSONString(poll));
System.out.println("现在的队列中元素为:"+ JSONObject.toJSONString(queue));
queue.add("4");
queue.add("4");
queue.add("4");
//这里remove(String s);使用的是Collection中的remove(String s)方法,并且只会移除上面的那个;
boolean remove1 = queue.remove("4");
System.out.println("poll移除的结果:"+ JSONObject.toJSONString(remove1));
System.out.println("现在的队列中元素为:"+ JSONObject.toJSONString(queue));
}catch (Exception e){
e.printStackTrace();
}
}
}
测试结果:
通过element()方法获取元素:1
通过peek()方法获取元素:1
现在的队列中元素为:["1","2"]
remove移除的结果为:"1"
现在的队列中元素为:["2"]
poll移除的结果:"2"
现在的队列中元素为:[]
poll移除的结果:true
现在的队列中元素为:["4","4"]
Process finished with exit code 0
②、add和offer区别
③、remove()和poll()测试
④、element()和peek()测试
欢迎关注我的公众号:幕桥社区