Java 8 | LinkedBlockingQueue spliterator() 方法与示例
LinkedBlockingQueue 的spliterator()方法返回与LinkedBlockingQueue相同元素的 Spliterator。返回的迭代器是弱一致的。它可以与Java 8 中的 Streams 一起使用。它也可以单独和批量遍历元素。
句法:
public Spliterator spliterator()
返回值:此方法返回 LinkedBlockingQueue 中元素的 Spliterator。
下面的程序说明了 LinkedBlockingQueue 类的 spliterator() 方法:
程序 1:从 LinkedBlockingQueue 创建一个拆分器,其中包含一个班级不同学生的姓名
Java
// Java Program Demonstrate spliterator()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
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("Aman");
linkedQueue.add("Amar");
linkedQueue.add("Sanjeet");
linkedQueue.add("Rabi");
// create Spliterator of linkedQueue
// using spliterator() method
Spliterator listOfNames = linkedQueue.spliterator();
// print result from Spliterator
System.out.println("list of names:");
// forEachRemaining method of Spliterator
listOfNames.forEachRemaining((n) -> System.out.println(n));
}
}
Java
// Java Program Demonstrate spliterator()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
public class GFG {
public void collectSplitator()
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue(capacityOfQueue);
// Add element to LinkedBlockingQueue
Employee emp1 = new Employee("Aman", "Blogger", "100000");
Employee emp2 = new Employee("Amar", "Manager", "99000");
// Add Employee Objects to linkedQueue
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// create Spliterator of linkedQueue
// using spliterator() method
Spliterator listOfEmp = linkedQueue.spliterator();
// print result from Spliterator
System.out.println("No of Employees = "
+ linkedQueue.size());
// forEachRemaining method of Spliterator
listOfEmp.forEachRemaining((n) -> print(n));
}
// print employee details
public void print(Employee e)
{
System.out.println("-----------------------------");
System.out.println("Employee Name : " + e.name);
System.out.println("Employee Position : " + e.position);
System.out.println("Employee Salary : " + e.salary);
}
// create an Employee Object with name,
// position and salary as attributes
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.collectSplitator();
}
}
输出:
list of names:
Aman
Amar
Sanjeet
Rabi
程序 2:从包含员工对象列表的 LinkedBlockingQueue 创建一个拆分器。
Java
// Java Program Demonstrate spliterator()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
public class GFG {
public void collectSplitator()
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue(capacityOfQueue);
// Add element to LinkedBlockingQueue
Employee emp1 = new Employee("Aman", "Blogger", "100000");
Employee emp2 = new Employee("Amar", "Manager", "99000");
// Add Employee Objects to linkedQueue
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// create Spliterator of linkedQueue
// using spliterator() method
Spliterator listOfEmp = linkedQueue.spliterator();
// print result from Spliterator
System.out.println("No of Employees = "
+ linkedQueue.size());
// forEachRemaining method of Spliterator
listOfEmp.forEachRemaining((n) -> print(n));
}
// print employee details
public void print(Employee e)
{
System.out.println("-----------------------------");
System.out.println("Employee Name : " + e.name);
System.out.println("Employee Position : " + e.position);
System.out.println("Employee Salary : " + e.salary);
}
// create an Employee Object with name,
// position and salary as attributes
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.collectSplitator();
}
}
输出:
No of Employees = 2
-----------------------------
Employee Name : Aman
Employee Position : Blogger
Employee Salary : 100000
-----------------------------
Employee Name : Amar
Employee Position : Manager
Employee Salary : 99000
参考: https: Java/util/concurrent/LinkedBlockingQueue.html#spliterator–