LinkedBlockingQueue的iterator()方法以正確的順序返回與此LinkedBlockingQueue相同的元素的迭代器。從此方法返回的元素按從LinkedBlockingQueue的first(head)到last(tail)的順序包含所有元素。返回的迭代器是弱一致性的。
用法:
public Iterator<E> iterator()
返回值:該方法按從first(head)到last(tail)的順序返回具有與LinkedBlockingQueue中存在的元素相同的迭代器。
以下示例程序旨在說明LinkedBlockingQueue類的iterator()方法:
示例1:從LinkedBlockingQueue創建一個Iteretor,其中包含班級不同學生的名字。
// 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<String> linkedQueue
= new LinkedBlockingQueue<String>(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<String> listOfNames = linkedQueue.iterator();
// print result
System.out.println("list of names:");
while (listOfNames.hasNext())
System.out.println(listOfNames.next());
}
}
輸出:
list of names: John Tom Clark Kat
示例2:從包含員工列表的LinkedBlockingQueue創建Iteretor。
// 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<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(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<Employee> 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#iterator–
相關用法
- Java LinkedBlockingQueue contains()用法及代碼示例
- Java LinkedBlockingQueue peek()用法及代碼示例
- Java LinkedBlockingQueue remainingCapacity()用法及代碼示例
- Java LinkedBlockingQueue remove()用法及代碼示例
- Java LinkedBlockingQueue put()用法及代碼示例
- Java LinkedBlockingQueue take()用法及代碼示例
- Java LinkedBlockingQueue size()用法及代碼示例
- Java LinkedBlockingQueue toArray()用法及代碼示例
- Java LinkedBlockingQueue poll()用法及代碼示例
- Java LinkedBlockingQueue drainTo()用法及代碼示例
- Java LinkedBlockingQueue toString()用法及代碼示例
- Java LinkedBlockingQueue clear()用法及代碼示例
- Java LinkedBlockingQueue toString()用法及代碼示例
- Java 8 LinkedBlockingQueue spliterator()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LinkedBlockingQueue iterator() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。