Java中的BlockingQueue remove()方法及其示例

Java中的BlockingQueue remove()方法及其示例

BlockingQueueremove(Object obj) 方法仅从该 BlockingQueue 中删除给定参数做为待删除元素的一个实例。如果此队列包含一个或多个元素 e,且 e 满足 obj.equals(e),则该方法删除一个这样的元素。如果此队列不包含该元素,则不执行任何操作。如果此队列包含了正在被删除的元素,则该方法返回 true 。

语法:

public boolean remove(Object o)

参数:

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

返回值:

如果此队列包含了正在被删除的元素,则该方法返回 true 。如果该 BlockingQueue 中不包含元素 obj ,则返回 false

注意: Java 中的 BlockingQueue remove() 方法是从 Queue 类继承而来的。

以下是演示 BlockingQueueremove(Object obj) 方法的示例程序:

程序 1:

//Java 程序演示 BlockingQueue 的 remove(Object obj) 方法
  
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
        throws InterruptedException
    {
        // 定义一个 BlockingQueue 的容量
        int capacityOfQueue = 4;
  
        // 创建 BlockingQueue 的对象
        BlockingQueue<String> BQ
            = new LinkedBlockingQueue<String>(capacityOfQueue);
  
        // 使用 put() 方法添加元素
        BQ.put("Karan");
        BQ.put("Suraj");
        BQ.put("Harsh");
        BQ.put("Rahul");
  
        // 打印队列中的元素
        System.out.println("Items in Queue are " + BQ);
  
        // 尝试使用 remove() 方法删除 Karan 元素
        boolean try1 = BQ.remove("Karan");
        // 打印 remove() 方法的结果
        System.out.println("String name Karan is removed :"
                           + try1);
  
        // 尝试使用 remove() 方法删除 Sunny 元素
        boolean try2 = BQ.remove("Sunny");
        // 打印 remove() 方法的结果
        System.out.println("String name Sunny is removed :"
                           + try2);
  
        // 尝试使用 remove() 方法删除 Harsh 元素
        boolean try3 = BQ.remove("Harsh");
        // 打印 remove() 方法的结果
        System.out.println("String name Harsh is removed :"
                           + try2);
  
        // 打印队列
        System.out.println("After Removing Some Elements:");
        System.out.println("Items in Queue are " + BQ);
    }
}
Items in Queue are [Karan, Suraj, Harsh, Rahul]
String name Karan is removed :true
String name Sunny is removed :false
String name Harsh is removed :false
After Removing Some Elements:
Items in Queue are [Suraj, Rahul]

程序 1:

// Java程序演示 BlockingQueue 的 remove(object obj) 方法
  
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
  
    public void removeDemo() throws InterruptedException
    {
        // 定义 BlockingQueue 的容量
        int capacityOfQueue = 5;
  
        // 创建 BlockingQueue 对象
        BlockingQueue<Employee> BQ
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
  
        // 向 LinkedBlockingQueue 中添加元素
        Employee emp1 = new Employee("Ranjeet", "测试人员", "29000", 27);
        Employee emp2 = new Employee("Sanjeet", "经理", "98000", 34);
        Employee emp3 = new Employee("Karan", "分析师", "44000", 30);
  
        // 使用 put(E e) 将 Employee 对象添加到 BQ 中
        BQ.put(emp1);
        BQ.put(emp2);
        BQ.put(emp3);
  
        // 输出 BQ 的详情
        System.out.println("移除元素前:");
        Iterator itr = BQ.iterator();
        while (itr.hasNext())
            System.out.println(itr.next());
  
        // 使用 remove(Object obj) 方法从 BQ 中移除名为 Sanjeet 的 emp2
        BQ.remove(emp2);
  
        // 使用 remove(Object obj) 方法从 BQ 中移除 emp1
        BQ.remove(emp1);
  
        // 输出 BQ 的详情
        System.out.println("移除部分元素后:");
        itr = BQ.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=测试人员, salary=29000, Age=27]
Employee [name=Sanjeet, position=经理, salary=98000, Age=34]
Employee [name=Karan, position=分析师, salary=44000, Age=30]
移除部分元素后:
Employee [name=Karan, position=分析师, salary=44000, Age=30]

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程