LinkedBlockingQueue 的 Java remove() 方法

LinkedBlockingQueue 的 Java remove() 方法

LinkedBlockingQueue 的 remove(Object obj) 方法仅从此 LinkedBlockingQueue 中删除传递的参数对象中的一个实例,如果存在的话。它将删除一个元素 e,使得 obj.equals(e),如果此队列包含一个或多个元素 e 的实例。如果此队列包含现在从 LinkedBlockingQueue 中删除的元素,则此方法返回 true。

语法:

public boolean remove(Object o)

参数: 此方法接受一个必须的参数 obj ,它是要从 LinkedBlockingQueue 中删除的元素。

返回值: 如果此队列包含现在从 LinkedBlockingQueue 中删除的元素,则此方法返回 true。如果 LinkedBlockingQueue 不包含元素 obj ,则此方法返回 false。

以下程序说明 LinkedBlockingQueue 类的 remove(Object obj) 方法:

程序1: 尝试使用 remove(Object obj) 从 LinkedBlockingQueue 中删除一些元素并打印结果。

// Java 程序演示 LinkedBlockingQueue 的 remove(Object obj) 方法
import java.util.concurrent.LinkedBlockingQueue;

public class GFG {
    public static void main(String[] args) throws InterruptedException {
        // 定义 LinkedBlockingQueue 的容量
        int capacityOfQueue = 4;

        // 创建 LinkedBlockingQueue 的对象
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);

        // 使用 put() 方法添加元素
        linkedQueue.put("Karan");
        linkedQueue.put("Suraj");
        linkedQueue.put("Harsh");
        linkedQueue.put("Rahul");

        // 打印队列元素
        System.out.println("队列中的项目为" + linkedQueue);

        // 尝试使用 remove() 将 Karan 从队列中删除
        boolean try1 = linkedQueue.remove("Karan");
        // 打印 remove() 的结果
        System.out.println("字符串名称 Karan 已删除:"
                           + try1);

        // 尝试使用 remove() 将 Sunny 从队列中删除
        boolean try2 = linkedQueue.remove("Sunny");
        // 打印 remove() 的结果
        System.out.println("字符串名称 Sunny 已删除:"
                           + try2);

        // 尝试使用 remove() 将 Harsh 从队列中删除
        boolean try3 = linkedQueue.remove("Harsh");
        // 打印 remove() 的结果
        System.out.println("字符串名称 Harsh 已删除:"
                           + try2);

        // 打印队列
        System.out.println("删除一些元素后:");
        System.out.println("队列中的项目为" + linkedQueue);
    }
}

输出:

队列中的项目为[Karan, Suraj, Harsh, Rahul]
字符串名称 Karan 已删除:true
字符串名称 Sunny 已删除:false
字符串名称 Harsh 已删除:false
删除一些元素后:
队列中的项目为[Suraj, Rahul]

程序2: 使用 LinkedBlockingQueue 的 remove(Object obj) 方法删除 Employee 对象。

//Java程序演示LinkedBlockingQueue类的remove(Object obj)方法
// 
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
 
    public void removeDemo() throws InterruptedException
    {
        //定义LinkedBlockingQueue的容量 
        int capacityOfQueue = 5;
 
        //创建LinkedBlockingQueue对象
        LinkedBlockingQueue<Employee> linkedQueue
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
 
        //向LinkedBlockingQueue添加元素    
        Employee emp1 = new Employee("Ranjeet", "Tester", "29000", 27);
        Employee emp2 = new Employee("Sanjeet", "Manager", "98000", 34);
        Employee emp3 = new Employee("Karan", "Analyst", "44000", 30);
 
        //使用put(E e)向linkedQueue添加Employee对象 
        linkedQueue.put(emp1);
        linkedQueue.put(emp2);
        linkedQueue.put(emp3);
 
        //输出linkedQueue的细节 
        System.out.println("在移除元素之前");
        Iterator itr = linkedQueue.iterator();
        while (itr.hasNext())
            System.out.println(itr.next());
 
        //使用remove(Object obj)方法从linkedQueue移除名字为Sanjeet的employee2 
        linkedQueue.remove(emp2);
 
        //也使用remove(Object obj)方法从LinkedBlockingQueue中移除Ranjeet employee1
        linkedQueue.remove(emp1);
 
        //输出linkedQueue的细节 
        System.out.println("在移除一些元素之后");
        itr = linkedQueue.iterator();
        while (itr.hasNext())
            System.out.println(itr.next());
    }
 
    //创建一个具有名字,职位,薪水和年龄属性的Employee对象 
    public class Employee {
 
        public String name;
        public String position;
        public String salary;
        public int Age;
        Employee(String name, String position,
                 String salary, int age)
        {
            this.name = name;
            this.position = position;
            this.salary = salary;
            this.Age = age;
        }
 
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary + ", Age=" + Age + "]";
        }
    }
 
    //主方法
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        try {
            gfg.removeDemo();
        }
        catch (InterruptedException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

输出结果:

在移除元素之前
Employee [name=Ranjeet, position=Tester, salary=29000, Age=27]
Employee [name=Sanjeet, position=Manager, salary=98000, Age=34]
Employee [name=Karan, position=Analyst, salary=44000, Age=30]
在移除一些元素之后
Employee [name=Karan, position=Analyst, salary=44000, Age=30]

参考链接: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#remove-java.lang.Object-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程