Java Program to Demonstrate the Spliterator() Method of LinkedList

You are currently viewing Java Program to Demonstrate the Spliterator() Method of LinkedList

Java program to demonstrate the spliterator() method of LinkedList

// Java program to demonstrate spliterator() method 
// of LinkedList

import java.util.*;

class Complex {
  int real, img;

  Complex(int r, int i) {
    real = r;
    img = i;
  }
}

public class Main {
  public static void main(String[] args) {
    int i = 0;
    LinkedList < Complex > list = new LinkedList < Complex > ();

    list.add(new Complex(10, 20));
    list.add(new Complex(20, 30));
    list.add(new Complex(30, 40));
    list.add(new Complex(40, 50));

    Spliterator < Complex > spliter = list.spliterator();

    // print result from Spliterator
    System.out.println("List items: ");
    spliter.forEachRemaining((val) -> printComplexNumber(val));
  }

  public static void printComplexNumber(Complex C) {
    System.out.println(C.real + " + " + C.img + "i");
  }
}

Output

List items: 
10 + 20i
20 + 30i
30 + 40i
40 + 50i

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply