- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fetch elements with iterator in Java?
Let us first create a List and add elements −
List<String>list = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });
Now, use Iterator to fetch each element −
Iterator<String>i = list.iterator();
Display the elements −
while (i.hasNext()) { System.out.println(i.next()); }
Example
import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Demo { public static void main(String[] a) { List<String>list = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" }); Iterator<String>i = list.iterator(); System.out.println("Displaying elements..."); while (i.hasNext()) { System.out.println(i.next()); } } }
Output
Displaying elements... P Q R S T U V W
- Related Articles
- Retrieving Elements from Collection in Java- Iterator
- How to use Iterator in Java?
- Loop through the Vector elements using an Iterator in Java
- How to iterate List using Iterator in Java?
- Iterator in Java
- Fetch elements of Java TreeSet using Iteration
- Difference between Iterator and Spilt Iterator in Java.
- Convert Iterator to Iterable in Java
- Iterator Functions in Java
- How to iterate a Java List using Iterator?
- Fetch a specific document in MongoDB with array elements
- Convert an Iterator to Stream in Java
- Iterator vs forEach in Java
- DoubleStream iterator() method in Java
- IntStream iterator() method in Java

Advertisements