Friday, December 27, 2024
Google search engine
HomeLanguagesJavaArrayDeque spliterator() method in Java

ArrayDeque spliterator() method in Java

The spliterator() method of ArrayDeque returns a Spliterator of the same elements as ArrayDeque but created Spliterator is late-binding and fail-fast. A late-binding Spliterator binds to the source of elements means ArrayDeque 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 ArrayDeque.

Below programs illustrate spliterator() method of ArrayDeque:

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




// Java Program Demonstrate spliterator()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of numbers
        ArrayDeque<Integer> list = new ArrayDeque<Integer>();
  
        // Add numbers to list
        list.add(1234);
        list.add(2345);
        list.add(3456);
        list.add(4567);
  
        // create Spliterator of ArrayDeque
        // using spliterator() method
        Spliterator<Integer> numbers = list.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Numbers:");
  
        // forEachRemaining method of Spliterator
        numbers.forEachRemaining((n) -> System.out.println(n));
    }
}


Output:

list of Numbers:
1234
2345
3456
4567

Example 2: To demonstrate spliterator() method on ArrayDeque which contains list of Strings.




// Java Program Demonstrate spliterator()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Strings
        ArrayDeque<String> list = new ArrayDeque<String>();
  
        // Add Strings to list
        // each string represents city name
        list.add("Kolkata");
        list.add("Delhi");
        list.add("Mumbai");
        list.add("Jaipur");
  
        // create Spliterator of ArrayDeque
        // using spliterator() method
        Spliterator<String> cities = list.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Cities:");
  
        // forEachRemaining method of Spliterator
        cities.forEachRemaining(
            (n) -> System.out.println("City Name: " + n));
    }
}


Output:

list of Cities:
City Name: Kolkata
City Name: Delhi
City Name: Mumbai
City Name: Jaipur

Reference:
https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#spliterator()

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments