Q:

Java program to demonstrate the spliterator() method of LinkedList

belongs to collection: Java LinkedList Programs

0

Java program to demonstrate the spliterator() method of LinkedList

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create a Complex class and then create the list of objects of the Complex class using LinkedList collection. Then we will use a spliterator() method to iterate LinkedList and print the result.

Program/Source Code:

The source code to demonstrate the spliterator() method of LinkedList is given below. The given program is compiled and executed successfully.

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

Explanation:

In the above program, we imported the "java.util.LinkedList" package to use the LinkedList collection class. Here, we created two classes Complex and Main. The Complex class contains two data members real and img. And, created a constructor to initialize data members. The Main class contains a main() method. The main() method is the entry point for the program.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java LinkedList Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program for a Calculator that can do three... >>
<< Java program to create a LinkedList collection of ...