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


Java LinkedList spliterator()用法及代碼示例


LinkedList的spliterator()方法返回一個後期綁定的分隔符,fail-fast具有與LinkedList相同的元素。將後期綁定的Spliterator綁定到元素的來源意味著LinkedList在第一次遍曆,第一次拆分或首次查詢時估計大小,而不是在創建Spliterator時。它可以與Java 8中的Streams一起使用。它也可以單獨和批量遍曆元素。分隔符是遍曆元素的更好方法,因為它提供了對元素的更多控製。

用法:

public Spliterator<E> spliterator()

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


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

示例1:演示LinkedList上的spliterator()方法,該方法包含對象列表。

// Java Program Demonstrate spliterator() 
// method of LinkedList 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an LinkedList which going to 
        // contains a list of numbers 
        LinkedList<Shape> shapes = new LinkedList<Shape>(); 
  
        // Add different shape to linkedlist 
        shapes.add(new Shape("Circle", 234)); 
        shapes.add(new Shape("Square", 225)); 
        shapes.add(new Shape("Cone", 543)); 
        shapes.add(new Shape("Rectangle", 342)); 
  
        // create Spliterator of LinkedList 
        // using spliterator() method 
        Spliterator<Shape> spliter = shapes.spliterator(); 
  
        // print result from Spliterator 
        System.out.println("list of Shapes:"); 
  
        // forEachRemaining method of Spliterator 
        spliter.forEachRemaining((Value) -> printDetails(Value)); 
    } 
  
    // print details 
    public static void printDetails(Shape s) 
    { 
        System.out.println("************************"); 
        System.out.println("Shape Name : " + s.shapename); 
        System.out.println("Shape Area : " + s.area); 
    } 
} 
  
// create a shape class 
class Shape { 
  
    // shape class has two attributes 
    String shapename; 
    int area; 
  
    public Shape(String shapename, int area) 
    { 
        super(); 
        this.shapename = shapename; 
        this.area = area; 
    } 
}
輸出:
list of Shapes:
************************
Shape Name : Circle
Shape Area : 234
************************
Shape Name : Square
Shape Area : 225
************************
Shape Name : Cone
Shape Area : 543
************************
Shape Name : Rectangle
Shape Area : 342

示例2:在LinkedList上演示spliterator()方法,該方法包含電影名稱列表。

// Java Program Demonstrate spliterator() 
// method of LinkedList 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an LinkedList which going to 
        // contains a list of Movie names which is actually 
        // string values 
        LinkedList<String> NameOfMovies = new LinkedList<String>(); 
  
        // Add Strings to list 
        // each string represents city name 
        NameOfMovies.add("Delhi 6"); 
        NameOfMovies.add("3 Idiots"); 
        NameOfMovies.add("Stree"); 
        NameOfMovies.add("Airlift"); 
  
        // using spliterator() method 
        Spliterator<String> names = NameOfMovies.spliterator(); 
  
        // print result from Spliterator 
        System.out.println("list of Movies:"); 
  
        // forEachRemaining method of Spliterator 
        names.forEachRemaining((n) -> System.out.println("Movie Name: " + n)); 
    } 
}
輸出:
list of Movies:
Movie Name: Delhi 6
Movie Name: 3 Idiots
Movie Name: Stree
Movie Name: Airlift

參考: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html#spliterator–



相關用法


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