Jumat, 11 Maret 2016

Six different ways to iterate list in java

How many ways we can iterate list in java?

  • We can iterate list in 6  different ways in java.
  1. For Loop
  2. Enhanced For Loop
  3. While Loop
  4. Iterator
  5. Collections stream() util (Java8 feature)
  6. ListIterator

different ways to iterate list in java


1. Iterate list using For loop


1.Basic Java example program to iterate arraylist elements using list iterator
  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class IterateList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12.  List<String> instanceofjavaList = new ArrayList<String>();
  13.        
  14. //Add elements to Arraylist
  15.  
  16. // add 5 different values to arraylist 
  17. instanceofjavaList.add("Interview Questions");
  18. instanceofjavaList.add("Interview Programs");
  19. instanceofjavaList.add("Concept and example program");
  20. instanceofjavaList.add("Concept and interview questions");
  21. instanceofjavaList.add("Java Quiz");
  22.     
  23. for (int i = 0; i < instanceofjavaList.size(); i++) {
  24.  
  25.     System.out.println(instanceofjavaList.get(i));
  26.  
  27. }

  28. }
  29.  
  30. }
     



Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz


 2. Iterate list using Enhanced For loop

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class IterateList{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create an ArrayList object
  11. List<String> instanceofjavaList = new ArrayList<String>();
  12.        
  13. //Add elements to Arraylist
  14.  
  15. // add 5 different values to arraylist 
  16. instanceofjavaList.add("Interview Questions");
  17. instanceofjavaList.add("Interview Programs");
  18. instanceofjavaList.add("Concept and example program");
  19. instanceofjavaList.add("Concept and interview questions");
  20. instanceofjavaList.add("Java Quiz");
  21.  
  22. for (String str: instanceofjavaList) {
  23.             System.out.println(str);
  24. }

  25. }
  26.  
  27. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz


 3. Iterate list using while loop

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class IterateList{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create an ArrayList object
  11. List<String> instanceofjavaList = new ArrayList<String>();
  12.        
  13. //Add elements to Arraylist
  14.  
  15. // add 5 different values to arraylist 
  16. instanceofjavaList.add("Interview Questions");
  17. instanceofjavaList.add("Interview Programs");
  18. instanceofjavaList.add("Concept and example program");
  19. instanceofjavaList.add("Concept and interview questions");
  20. instanceofjavaList.add("Java Quiz");
  21.  
  22.  
  23. int i = 0;
  24.  
  25. while (i < instanceofjavaList.size()) {
  26.  
  27.   System.out.println(instanceofjavaList.get(i));
  28.   i++;
  29. }

  30. }
  31.  
  32. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz

 4. Iterate list using iterator

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. public class IterateList{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create an ArrayList object
  11. List<String> instanceofjavaList = new ArrayList<String>();
  12.        
  13. //Add elements to Arraylist
  14.  
  15. // add 5 different values to arraylist 
  16. instanceofjavaList.add("Interview Questions");
  17. instanceofjavaList.add("Interview Programs");
  18. instanceofjavaList.add("Concept and example program");
  19. instanceofjavaList.add("Concept and interview questions");
  20. instanceofjavaList.add("Java Quiz");
  21.  
  22.  
  23. Iterator<String> itr = instanceofjavaList.iterator();
  24.  
  25. while (itr.hasNext()) {
  26.      System.out.println(itr.next());
  27. }
  28.  
  29. }
  30.  
  31. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz


 5. Iterate list using Stream API.

  1. package com.javaIteratelist;
  2.  
  3. import java.util.ArrayList;
  4. public class IterateList{
  5.  
  6. public static void main(String[] args) {
  7.   
  8. //create an ArrayList object
  9. List<String> instanceofjavaList = new ArrayList<String>();
  10.        
  11. //Add elements to Arraylist
  12.  
  13. // add 5 different values to arraylist 
  14. instanceofjavaList.add("Interview Questions");
  15. instanceofjavaList.add("Interview Programs");
  16. instanceofjavaList.add("Concept and example program");
  17. instanceofjavaList.add("Concept and interview questions");
  18. instanceofjavaList.add("Java Quiz");
  19.  
  20.  
  21. instanceofjavaList.forEach((name) -> {
  22.             System.out.println(name);
  23.         }); 

  24. }
  25.  
  26. }
     


Output:

  1. Interview Questions
  2. Interview Programs
  3. Concept and example program
  4. Concept and interview questions
  5. Java Quiz

6. Using List erator:

You Might Like:

1.What are all the different places to create object in java

Bagikan

Jangan lewatkan

Six different ways to iterate list in java
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.