How many ways we can iterate list in java?
1. Iterate list using For loop
2. Iterate list using Enhanced For loop
Output:
3. Iterate list using while loop
Output:
4. Iterate list using iterator
Output:
5. Iterate list using Stream API.
Output:
6. Using List erator:
You Might Like:
1.What are all the different places to create object in java
2.Java Example program to restrict a class from creating not more than three objects
- We can iterate list in 6 different ways in java.
- For Loop
- Enhanced For Loop
- While Loop
- Iterator
- Collections stream() util (Java8 feature)
- ListIterator
1. Iterate list using For loop
1.Basic Java example program to iterate arraylist elements using list iterator
Output:
- package com.javaIteratelist;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- public class IterateList{
- public static void main(String[] args) {
- //create an ArrayList object
- List<String> instanceofjavaList = new ArrayList<String>();
- //Add elements to Arraylist
- // add 5 different values to arraylist
- instanceofjavaList.add("Interview Questions");
- instanceofjavaList.add("Interview Programs");
- instanceofjavaList.add("Concept and example program");
- instanceofjavaList.add("Concept and interview questions");
- instanceofjavaList.add("Java Quiz");
- for (int i = 0; i < instanceofjavaList.size(); i++) {
- System.out.println(instanceofjavaList.get(i));
- }
- }
- }
Output:
- Interview Questions
- Interview Programs
- Concept and example program
- Concept and interview questions
- Java Quiz
2. Iterate list using Enhanced For loop
- package com.javaIteratelist;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class IterateList{
- public static void main(String[] args) {
- //create an ArrayList object
- List<String> instanceofjavaList = new ArrayList<String>();
- //Add elements to Arraylist
- // add 5 different values to arraylist
- instanceofjavaList.add("Interview Questions");
- instanceofjavaList.add("Interview Programs");
- instanceofjavaList.add("Concept and example program");
- instanceofjavaList.add("Concept and interview questions");
- instanceofjavaList.add("Java Quiz");
- for (String str: instanceofjavaList) {
- System.out.println(str);
- }
- }
- }
Output:
- Interview Questions
- Interview Programs
- Concept and example program
- Concept and interview questions
- Java Quiz
3. Iterate list using while loop
- package com.javaIteratelist;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class IterateList{
- public static void main(String[] args) {
- //create an ArrayList object
- List<String> instanceofjavaList = new ArrayList<String>();
- //Add elements to Arraylist
- // add 5 different values to arraylist
- instanceofjavaList.add("Interview Questions");
- instanceofjavaList.add("Interview Programs");
- instanceofjavaList.add("Concept and example program");
- instanceofjavaList.add("Concept and interview questions");
- instanceofjavaList.add("Java Quiz");
- int i = 0;
- while (i < instanceofjavaList.size()) {
- System.out.println(instanceofjavaList.get(i));
- i++;
- }
- }
- }
Output:
- Interview Questions
- Interview Programs
- Concept and example program
- Concept and interview questions
- Java Quiz
4. Iterate list using iterator
- package com.javaIteratelist;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class IterateList{
- public static void main(String[] args) {
- //create an ArrayList object
- List<String> instanceofjavaList = new ArrayList<String>();
- //Add elements to Arraylist
- // add 5 different values to arraylist
- instanceofjavaList.add("Interview Questions");
- instanceofjavaList.add("Interview Programs");
- instanceofjavaList.add("Concept and example program");
- instanceofjavaList.add("Concept and interview questions");
- instanceofjavaList.add("Java Quiz");
- Iterator<String> itr = instanceofjavaList.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
Output:
- Interview Questions
- Interview Programs
- Concept and example program
- Concept and interview questions
- Java Quiz
5. Iterate list using Stream API.
- package com.javaIteratelist;
- import java.util.ArrayList;
- public class IterateList{
- public static void main(String[] args) {
- //create an ArrayList object
- List<String> instanceofjavaList = new ArrayList<String>();
- //Add elements to Arraylist
- // add 5 different values to arraylist
- instanceofjavaList.add("Interview Questions");
- instanceofjavaList.add("Interview Programs");
- instanceofjavaList.add("Concept and example program");
- instanceofjavaList.add("Concept and interview questions");
- instanceofjavaList.add("Java Quiz");
- instanceofjavaList.forEach((name) -> {
- System.out.println(name);
- });
- }
- }
Output:
- Interview Questions
- Interview Programs
- Concept and example program
- Concept and interview questions
- Java Quiz
6. Using List erator:
You Might Like:
1.What are all the different places to create object in java
2.Java Example program to restrict a class from creating not more than three objects
Bagikan
Six different ways to iterate list in java
4/
5
Oleh
Kris Kimcil