Java BlockingQueue remove()方法及示例

Java BlockingQueue remove()方法及示例

BlockingQueueremove(Object obj) 方法只从这个BlockingQueue中移除一个作为参数传递的给定对象的实例,如果它存在的话。如果这个队列包含一个或多个元素e的实例,它将删除一个元素e,即obj.eals(e),如果这个队列包含现在从BlockingQueue中删除的元素,该方法将返回true。

语法

public boolean remove(Object o)

参数: 该方法接受一个强制性参数obj,它是要从BlockingQueue中移除的元素。

返回值: 如果该队列包含现在从BlockingQueue中移除的元素,该方法返回true。如果BlockingQueue不包含obj 这个元素,那么这个方法返回false。

注意BlockingQueueremove() 方法已经从Java的 Queue 类中继承了。

下面的程序说明了BlockingQueue类的remove(Object obj)方法。

程序1:

// Java Program Demonstrate  remove(Object obj)
// method of BlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of BlockingQueue
        int capacityOfQueue = 4;
  
        // create object of BlockingQueue
        BlockingQueue<String> BQ
            = new LinkedBlockingQueue<String>(capacityOfQueue);
  
        // Add element using put() method
        BQ.put("Karan");
        BQ.put("Suraj");
        BQ.put("Harsh");
        BQ.put("Rahul");
  
        // print elements of queue
        System.out.println("Items in Queue are " + BQ);
  
        // try to remove Karan from Queue using remove()
        boolean try1 = BQ.remove("Karan");
        // Print result of remove()
        System.out.println("String name Karan is removed :"
                           + try1);
  
        // try to remove Sunny from Queue using remove()
        boolean try2 = BQ.remove("Sunny");
        // Print result of remove()
        System.out.println("String name Sunny is removed :"
                           + try2);
  
        // try to remove Sunny from Queue using remove()
        boolean try3 = BQ.remove("Harsh");
        // Print result of remove()
        System.out.println("String name Harsh is removed :"
                           + try2);
  
        // print queue
        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 Program Demonstrate remove(object obj)
// method of BlockingQueue
  
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
  
    public void removeDemo() throws InterruptedException
    {
        // define capacity of BlockingQueue
        int capacityOfQueue = 5;
  
        // create object of BlockingQueue
        BlockingQueue<Employee> BQ
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
  
        // Add element to 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);
  
        // Add Employee Objects to BQ Using put(E e)
        BQ.put(emp1);
        BQ.put(emp2);
        BQ.put(emp3);
  
        // print details of BQ
        System.out.println("Before removing Elements");
        Iterator itr = BQ.iterator();
        while (itr.hasNext())
            System.out.println(itr.next());
  
        // remove employee2 name Sanjeet from BQ
        // Using remove(Object obj) method
        BQ.remove(emp2);
  
        // Also remove Ranjeet employee1 from BQ
        // Using remove(Object obj) method
        BQ.remove(emp1);
  
        // print details of BQ
        System.out.println("After removing Some Elements");
        itr = BQ.iterator();
        while (itr.hasNext())
            System.out.println(itr.next());
    }
  
    // create an Employee Object with name,
    // position, salary and age as attributes
    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 + "]";
        }
    }
  
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        try {
            gfg.removeDemo();
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

输出。

Before removing Elements
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]
After removing Some Elements
Employee [name=Karan, position=Analyst, salary=44000, Age=30]

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程