LinkedList spliterator() method in Java

The spliterator() method of LinkedList returns a Spliterator which is late-binding and fail-fast with the same elements as LinkedList. A late-binding Spliterator binds to the source of elements means LinkedList at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliterator is created. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too. Spliterator is better way to traverse over element because it provides more control on elements.

Syntax:

public Spliterator<E> spliterator()

Returns: This method returns a Spliterator over the elements in LinkedList.

Below programs illustrate spliterator() method of LinkedList:

Example 1: To demonstrate spliterator() method on LinkedList which contains a list of objects.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
    }
}

chevron_right


Output:

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

Example 2: To demonstrate spliterator() method on LinkedList which contains list of Movie Names.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

list of Movies:
Movie Name: Delhi 6
Movie Name: 3 Idiots
Movie Name: Stree
Movie Name: Airlift

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.