Java中的队列offer()方法

Java中的队列offer()方法

Queue接口offer(E e) 方法可以在不违反容量限制的情况下立即将指定的元素插入此队列。与add()方法相比,当容器的容量已满时,此方法不会抛出异常而是返回false,因此该方法更加优越。

语法:

boolean offer(E e)

参数: 此方法接受一个强制性参数 e ,它是要插入队列中的元素。

返回值: 如果成功插入,则此方法返回true,否则返回false。

异常: 该函数会抛出四个异常,这些异常描述如下:

  • ClassCastException :要插入的元素的类防止其被添加到此容器中时。
  • IllegalArgumentException :由于元素的某些属性,使其无法添加到队列中。
  • NullPointerException :当要插入的元素是空并且Queue接口不允许空元素时。

以下程序演示了Queue的offer()方法:

程序1: 使用 LinkedBlockingDeque

//Java程序演示offer()方法
// of Queue
  
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        //创造Queue对象
        Queue<Integer> Q
            = new LinkedBlockingQueue<Integer>(3);
  
        if (Q.offer(10))
            System.out.println("队列未满"
                               + ",并插入了10");
        else
            System.out.println("队列已满");
  
        if (Q.offer(15))
            System.out.println("队列未满"
                               + ",并插入了15");
        else
            System.out.println("队列已满");
  
        if (Q.offer(25))
            System.out.println("队列未满"
                               + ",并插入了25");
        else
            System.out.println("队列已满");
  
        if (Q.offer(20))
            System.out.println("队列未满"
                               + ",并插入了20");
        else
            System.out.println("队列已满");
  
        //在移除之前打印Queue
        System.out.println("队列:" + Q);
    }
}
队列未满,插入了10
队列未满,插入了15
队列未满,插入了25
队列已满
队列:[10, 15, 25]

程序2: 使用 ConcurrentLinkedDeque

// Java程序演示Queue的offer()方法
// 
  
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // 创建Queue对象
        Queue<Integer> Q
            = new ConcurrentLinkedDeque<Integer>();
  
        if (Q.offer(10))
            System.out.println("队列未满,插入了10");
        else
            System.out.println("队列已满");
  
        if (Q.offer(15))
            System.out.println("队列未满,插入了15");
        else
            System.out.println("队列已满");
  
        if (Q.offer(25))
            System.out.println("队列未满,插入了25");
        else
            System.out.println("队列已满");
  
        if (Q.offer(20))
            System.out.println("队列未满,插入了20");
        else
            System.out.println("队列已满");
  
        // 删除前打印队列
        System.out.println("Queue: " + Q);
    }
}
队列未满,插入了10
队列未满,插入了15
队列未满,插入了25
队列未满,插入了20
Queue: [10, 15, 25, 20]

程序3: 使用 ArrayDeque 实现。

// Java程序演示Queue的offer()方法
// 
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // 创建Queue对象
        Queue<Integer> Q
            = new ArrayDeque<Integer>(6);
  
        if (Q.offer(10))
            System.out.println("队列未满,插入了10");
        else
            System.out.println("队列已满");
  
        if (Q.offer(15))
            System.out.println("队列未满,插入了15");
        else
            System.out.println("队列已满");
  
        if (Q.offer(25))
            System.out.println("队列未满,插入了25");
        else
            System.out.println("队列已满");
  
        if (Q.offer(20))
            System.out.println("队列未满,插入了20");
        else
            System.out.println("队列已满");
  
        // 删除前打印队列
        System.out.println("Queue: " + Q);
    }
}
队列未满,插入了10
队列未满,插入了15
队列未满,插入了25
队列未满,插入了20
Queue: [10, 15, 25, 20]

程序4: 使用 LinkedList 实现。

// Java程序演示offer()
//方法的Queue
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        //创建Queue对象
        Queue<Integer> Q
            = new LinkedList<Integer>();
  
        if (Q.offer(10))
            System.out.println("队列未满,插入10");
        else
            System.out.println("队列已满");
  
        if (Q.offer(15))
            System.out.println("队列未满,插入15");
        else
            System.out.println("队列已满");
  
        if (Q.offer(25))
            System.out.println("队列未满,插入25");
        else
            System.out.println("队列已满");
  
        if (Q.offer(20))
            System.out.println("队列未满,插入20");
        else
            System.out.println("队列已满");
  
        //在删除之前打印Queue
        System.out.println("Queue: " + Q);
    }
}
队列未满,插入10
队列未满,插入15
队列未满,插入25
队列未满,插入20
Queue: [10, 15, 25, 20]

下面的程序说明此方法抛出的异常:

程序5: 展示 NullPointerException

// Java程序演示offer()
//当传递Null时的Queue方法
  
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
    public static void main(String[] args)
        throws NullPointerException
    {
  
        //创建Queue对象
        Queue<Integer> Q
            = new LinkedBlockingQueue<Integer>();
  
        //将数字添加到Deque的末尾
        Q.offer(7855642);
        Q.offer(35658786);
        Q.offer(5278367);
  
        try {
            //插入null时
            Q.offer(null);
        }
        catch (Exception e) {
            System.out.println("异常:" + e);
        }
    }
}
异常:java.lang.NullPointerException

注意: 其他两个异常是内部异常,由编译器引起,因此不能在代码中显示。

参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#offer-E-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程