Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

LinkedList spliterator() method in Java

  • Last Updated : 10 Dec, 2018

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.




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




// 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));
    }
}
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
Recommended Articles
Page :

Start Your Coding Journey Now!