📌  相关文章
📜  Java中的 LinkedBlockingQueue iterator() 方法

📅  最后修改于: 2022-05-13 01:54:44.298000             🧑  作者: Mango

Java中的 LinkedBlockingQueue iterator() 方法

LinkedBlockingQueue 的iterator()方法以正确的顺序返回与此 LinkedBlockingQueue 相同元素的迭代器。此方法返回的元素包含 LinkedBlockingQueue 的first(head)last(tail)的所有元素。返回的迭代器是弱一致的。
句法:

public Iterator iterator()

返回值:该方法返回具有与 LinkedBlockingQueue 中存在的相同元素的迭代器,从 first(head) 到 last(tail) 的顺序。
下面的程序说明了 LinkedBlockingQueue 类的 iterator() 方法:
程序 1:从 LinkedBlockingQueue 创建一个迭代器,其中包含一个班级不同学生的名字。

Java
// Java Program Demonstrate iterator()
// method of LinkedBlockingQueue
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 7;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
 
        // Add element to LinkedBlockingQueue
        linkedQueue.add("John");
        linkedQueue.add("Tom");
        linkedQueue.add("Clark");
        linkedQueue.add("Kat");
 
        // create Iterator of linkedQueue using iterator() method
        Iterator listOfNames = linkedQueue.iterator();
 
        // print result
        System.out.println("list of names:");
        while (listOfNames.hasNext())
            System.out.println(listOfNames.next());
    }
}


Java
// Java Program Demonstrate iterator()
// method of LinkedBlockingQueue
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
 
public class GFG {
 
    public void collectIterator()
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 7;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
 
        // Add element to LinkedBlockingQueue
        Employee emp1 = new Employee("Sachin", "Developer", "39000");
        Employee emp2 = new Employee("Sanjeev", "Tester", "26000");
 
        // Add Employee Objects to linkedQueue
        linkedQueue.add(emp1);
        linkedQueue.add(emp2);
 
        // create Iterator of linkedQueue using iterator() method
        Iterator listOfEmployee = linkedQueue.iterator();
 
        // print result from iterator
        System.out.println("list of Employees:");
        while (listOfEmployee.hasNext()) {
            System.out.println("*************************");
            Employee emp = listOfEmployee.next();
            System.out.println("Employee Name : " + emp.name);
            System.out.println("Employee Position : " + emp.position);
            System.out.println("Employee Salary : " + emp.salary);
        }
    }
 
    // create an Employee Object with name,
    // position and salary as an attribute
    public class Employee {
 
        public String name;
        public String position;
        public String salary;
 
        Employee(String name, String position, String salary)
        {
            this.name = name;
            this.position = position;
            this.salary = salary;
        }
 
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary + "]";
        }
    }
 
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.collectIterator();
    }
}


输出:
list of names:
John
Tom
Clark
Kat

程序 2:从包含员工列表的 LinkedBlockingQueue 创建一个迭代器。

Java

// Java Program Demonstrate iterator()
// method of LinkedBlockingQueue
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
 
public class GFG {
 
    public void collectIterator()
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 7;
 
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
 
        // Add element to LinkedBlockingQueue
        Employee emp1 = new Employee("Sachin", "Developer", "39000");
        Employee emp2 = new Employee("Sanjeev", "Tester", "26000");
 
        // Add Employee Objects to linkedQueue
        linkedQueue.add(emp1);
        linkedQueue.add(emp2);
 
        // create Iterator of linkedQueue using iterator() method
        Iterator listOfEmployee = linkedQueue.iterator();
 
        // print result from iterator
        System.out.println("list of Employees:");
        while (listOfEmployee.hasNext()) {
            System.out.println("*************************");
            Employee emp = listOfEmployee.next();
            System.out.println("Employee Name : " + emp.name);
            System.out.println("Employee Position : " + emp.position);
            System.out.println("Employee Salary : " + emp.salary);
        }
    }
 
    // create an Employee Object with name,
    // position and salary as an attribute
    public class Employee {
 
        public String name;
        public String position;
        public String salary;
 
        Employee(String name, String position, String salary)
        {
            this.name = name;
            this.position = position;
            this.salary = salary;
        }
 
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary + "]";
        }
    }
 
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.collectIterator();
    }
}
输出:
list of Employees:
*************************
Employee Name : Sachin
Employee Position : Developer
Employee Salary : 39000
*************************
Employee Name : Sanjeev
Employee Position : Tester
Employee Salary : 26000

参考: https: Java/util/concurrent/LinkedBlockingQueue.html#iterator–