当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。