java queue iterator_Java LinkedBlockingQueue iterator()用法及代码示例

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

用法:

public Iterator 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 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());

}

}

输出:

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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值