📜  Java中的 LinkedList spliterator() 方法

📅  最后修改于: 2022-05-13 01:55:27.103000             🧑  作者: Mango

Java中的 LinkedList spliterator() 方法

LinkedListspliterator()方法返回一个Spliterator ,它具有与 LinkedList 相同的元素,是后期绑定和快速失败的。后期绑定 Spliterator 绑定到元素源意味着 LinkedList 在第一次遍历、第一次拆分或第一次查询估计大小时,而不是在创建 Spliterator 时。它可以与Java 8 中的 Streams 一起使用。它也可以单独和批量遍历元素。 Spliterator 是遍历元素的更好方法,因为它提供了对元素的更多控制。

句法:

public Spliterator 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 shapes = new LinkedList();
  
        // 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 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 NameOfMovies = new LinkedList();
  
        // 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 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: Java/util/LinkedList.html#spliterator–