The Artima Developer Community
Sponsored Link

Java Buzz Forum
5 different ways to iterate list in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
5 different ways to iterate list in java Posted: Mar 11, 2016 7:44 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: 5 different ways to iterate list in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
How many ways we can iterate list in java?

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

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

Read: 5 different ways to iterate list in java

Topic: Push your configuration to the limit – spice it up with statistics Previous Topic   Next Topic Topic: Microservices trouble? Lagom is here to help. Try it!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use