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


Java 8 LinkedBlockingQueue spliterator()用法及代碼示例


LinkedBlockingQueue的spliterator()方法返回與LinkedBlockingQueue相同元素的分隔符。返回的迭代器是弱一致性的。它可以與Java 8中的Streams一起使用。它也可以單獨和批量遍曆元素。

用法:

public Spliterator spliterator()

返回值:此方法在LinkedBlockingQueue中的元素上返回一個Spliterator。


以下示例程序旨在說明LinkedBlockingQueue類的spliterator()方法:

程序1:從LinkedBlockingQueue創建一個拆分器,該拆分器包含班級不同學生的姓名

// 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<String> linkedQueue 
            = new LinkedBlockingQueue<String>(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<String> listOfNames = linkedQueue.spliterator(); 
  
        // print result from Spliterator 
        System.out.println("list of names:"); 
  
        // forEachRemaining method  of Spliterator 
        listOfNames.forEachRemaining((n) -> System.out.println(n)); 
    } 
}
輸出:
list of names:
Aman
Amar
Sanjeet
Rabi

程序2:從LinkedBlockingQueue創建一個Splitter,其中包含Employees對象的列表。

// 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<Employee> linkedQueue 
            = new LinkedBlockingQueue<Employee>(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<Employee> 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 emplyee 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#spliterator-



相關用法


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