當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LinkedBlockingQueue remove()用法及代碼示例


LinkedBlockingQueue的remove(Object obj)方法僅從此LinkedBlockingQueue中移除作為參數傳遞的給定Object的一個實例(如果存在)。如果此隊列包含元素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 Program Demonstrate  remove(Object obj) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 4; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<String> linkedQueue 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // Add element using put() method 
        linkedQueue.put("Karan"); 
        linkedQueue.put("Suraj"); 
        linkedQueue.put("Harsh"); 
        linkedQueue.put("Rahul"); 
  
        // print elements of queue 
        System.out.println("Items in Queue are " + linkedQueue); 
  
        // try to remove Karan from Queue using remove() 
        boolean try1 = linkedQueue.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 = linkedQueue.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 = linkedQueue.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 " + linkedQueue); 
    } 
}
輸出:
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:使用LinkedBlockingQueue中的remove(Object obj)方法刪除Employee對象

// Java Program Demonstrate remove(object obj) 
// method of LinkedBlockingQueue 
  
import java.util.Iterator; 
import java.util.concurrent.LinkedBlockingQueue; 
public class GFG { 
  
    public void removeDemo() throws InterruptedException 
    { 
        // define capacity of LinkedBlockingQueue 
        int capacityOfQueue = 5; 
  
        // create object of LinkedBlockingQueue 
        LinkedBlockingQueue<Employee> linkedQueue 
            = 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 linkedQueue Using put(E e) 
        linkedQueue.put(emp1); 
        linkedQueue.put(emp2); 
        linkedQueue.put(emp3); 
  
        // print details of linkedQueue 
        System.out.println("Before removing Elements"); 
        Iterator itr = linkedQueue.iterator(); 
        while (itr.hasNext()) 
            System.out.println(itr.next()); 
  
        // remove employee2 name Sanjeet from linkedQueue 
        // Using remove(Object obj) method 
        linkedQueue.remove(emp2); 
  
        // Also remove Ranjeet employee1 from linkedQueue 
        // Using remove(Object obj) method 
        linkedQueue.remove(emp1); 
  
        // print details of linkedQueue 
        System.out.println("After removing Some Elements"); 
        itr = linkedQueue.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/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#remove-java.lang.Object-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LinkedBlockingQueue remove() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。