Java LinkedTransferQueue remove()方法

Java LinkedTransferQueue remove()方法

java.util.concurrent.LinkedTransferQueue.remove() 方法是Java中的一个内置函数,如果一个元素存在于这个队列中,它就会被删除。

语法

LinkedTransferQueue.remove(Object o)

参数: 该函数接受一个参数,即要删除的对象。

返回值: 该函数在成功删除对象时返回一个true的布尔值,否则返回false。

下面的程序说明了LinkedTransferQueue.remove()方法。

程序1: 要删除的元素存在于队列中。

// Java Program Demonstrate remove()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueRemoveExample1 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
              new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 1; i <= 5; i++)
            queue.add(i);
  
        // Printing the elements of the queue
        System.out.println("The elements in the queue are:");
        for (Integer i : queue)
            System.out.print(i + " ");
  
        // remove() method will remove the specified
        // element from the queue
        queue.remove(1);
        queue.remove(5);
  
        // Printing the elements of the queue
        System.out.println("\nRemaining elements in queue : ");
        for (Integer i : queue)
            System.out.print(i + " ");
    }
}

输出:

The elements in the queue are:
1 2 3 4 5 
Remaining elements in queue : 
2 3 4

程序2: 要删除的元素在队列中不存在。

// Java Program Demonstrate remove()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueueRemoveExample2 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
                    new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 10; i <= 15; i++)
            queue.add(i);
  
        // Printing the elements of the queue
        System.out.println("The elements in the queue are:");
        for (Integer i : queue)
            System.out.print(i + " ");
  
        // remove() method will remove the specified
        // element from the queue
        queue.remove(1);
        queue.remove(5);
  
        // Printing the elements of the queue
        System.out.println("\nRemaining elements in queue : ");
        for (Integer i : queue)
            System.out.print(i + " ");
    }
}

输出:

The elements in the queue are:
10 11 12 13 14 15 
Remaining elements in queue : 
10 11 12 13 14 15

参考资料 : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#remove(java.lang.Object)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程