
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
IntStream Iterator Method in Java
The iterator() method of the IntStream class in Java is used to return an iterator for the elements of this stream.
The syntax is as follows
PrimitiveIterator.OfInt iterator()
Here, PrimitiveIterator.OfInt is an Iterator specialized for int values. To work with the IntStream class in Java, import the following package
import java.util.stream.IntStream;
Create an IntStream and add some elements
IntStream intStream = IntStream.of(15, 40, 55, 70, 95, 120);
To return an iterator for the stream elements
PrimitiveIterator.OfInt primIterator = intStream.iterator();
The following is an example to implement IntStream iterator() method in Java
Example
import java.util.*; import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream intStream = IntStream.of(15, 40, 55, 70, 95, 120); PrimitiveIterator.OfInt primIterator = intStream.iterator(); while (primIterator.hasNext()) { System.out.println(primIterator.nextInt()); } } }
Output
15 40 55 70 95 120
Advertisements