Java Queue offer()方法
Queue Interface 的 offer(E e) 方法在不违反容量限制的情况下可以立即插入指定的元素到这个队列中。这个方法比add()方法更好,因为这个方法不会在容器容量满时抛出一个异常,因为它返回false。
语法
boolean offer(E e)
参数: 该方法接受一个强制性参数 e ,它是要插入队列中的元素。
返回: 该方法在成功插入时返回true,否则返回false。
异常: 该函数抛出四个异常,描述如下。
- ClassCastException :当要输入的元素的类别阻止它被添加到这个容器中。
- IllegalArgumentException :当元素的某些属性阻止它被添加到队列中。
- NullPointerException :当要插入的元素被传递为空,而队列的接口不允许空元素。
以下程序说明了队列的offer()方法。
程序1: 在 LinkedBlockingDeque 的帮助下 。
// Java Program Demonstrate offer()
// method of Queue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>(3);
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
输出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is full
Queue: [10, 15, 25]
程序2: 在 ConcurrentLinkedDeque 的帮助下 。
// Java Program Demonstrate offer()
// method of Queue
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new ConcurrentLinkedDeque<Integer>();
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
输出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is not full and 20 is inserted
Queue: [10, 15, 25, 20]
程序3: 在 ArrayDeque 的帮助下 。
// Java Program Demonstrate offer()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new ArrayDeque<Integer>(6);
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
输出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is not full and 20 is inserted
Queue: [10, 15, 25, 20]
程序4: 在 LinkedList 的帮助下 。
// Java Program Demonstrate offer()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedList<Integer>();
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
输出:
The Queue is not full and 10 is inserted
The Queue is not full and 15 is inserted
The Queue is not full and 25 is inserted
The Queue is not full and 20 is inserted
Queue: [10, 15, 25, 20]
下面的程序说明了由这个方法抛出的异常。
程序5: 显示 NullPointerException.
// Java Program Demonstrate offer()
// method of Queue when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws NullPointerException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>();
// Add numbers to end of Deque
Q.offer(7855642);
Q.offer(35658786);
Q.offer(5278367);
try {
// when null is inserted
Q.offer(null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Exception: java.lang.NullPointerException
注意: 另外两个异常是内部的,取决于编译器,因此不能在代码中显示。
参考资料 :https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#offer-E-