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

Java program to insert an element to ArrayList using ListIterator Example

1.Basic Java example program to insert element using list iterator to arraylist
  1. package com.javaIteratearraylistiterator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class IterateListIteratorArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12. ArrayList<String> arrayList = new ArrayList();
  13.        
  14. //Add elements to Arraylist
  15.  
  16. arrayList.add("A");
  17. arrayList.add("B");
  18. arrayList.add("C");
  19. arrayList.add("D");
  20. arrayList.add("F");
  21. arrayList.add("F");
  22. arrayList.add("G");
  23. arrayList.add("H");
  24. arrayList.add("I");
  25.     
  26.         
  27.  /*
  28. Get a ListIterator object for ArrayList using
  29. istIterator() method.
  30. */
  31.   
  32. System.out.println("Before inserting element");
  33.  
  34. for(int intIndex = 0; intIndex < arrayList.size(); intIndex++)
  35.               System.out.println(arrayList.get(intIndex)); 
  36. ListIterator itr = arrayList.listIterator();
  37.        
  38. /*
  39.       Use void add(Object o) method of ListIterator to add or insert an element
  40.       to List. It adds an element just before the element that would have
  41.       been returned by next method call and after the element that would have
  42.       returned by previous call.
  43.     */
  44.    
  45.     itr .next();
  46.        
  47.     //Add an element
  48.     itr .add("Added Element");
  49.     /*
  50.  
  51. System.out.println("After inserting element .");
  52.  
  53. for(int intIndex = 0; intIndex < arrayList.size(); intIndex++)
  54.               System.out.println(arrayList.get(intIndex));   
  55.  
  56. }
  57. }
     



Output:

  1. Before inserting element
  2. A
  3. B
  4. C
  5. D
  6. F
  7. F
  8. G
  9. H
  10. I
  11. After inserting element .
  12. A
  13. Added Element
  14. B
  15. C
  16. D
  17. F
  18. F
  19. G
  20. H
  21. I

Java example program to print message without using System.out.println()

Is it possible to print message without using system.out.println?


  • Yes its possible to print message without using System.out.println("");

  1. System.out.write("www.instanceofjava.com \n".getBytes());
  2.  System.out.format("%s", "www.instanceofjava.com \n")
  3.  PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
       myout.print("www.instanceofjava.com \n");
  4. System.err.print("This is custom error message");
  5. System.console().writer().println("Hai");

print without system.out.println()



1.System.out.write(Byte [] arg0);

  • System.out.write(Byte [] arg0) method The java.io.FilterOutputStream.write(byte[] b) method writes b.length bytes to this output stream.

  1. package com.instanceofjava;
  2.  
  3. public class PrintMessage {

  4. public static void main(String[] args) throws IOException{
  5.  
  6.        System.out.write("Hello World".getBytes());
  7. }
  8. }

Output:

  1. Hello World

2. PrintStream  java.io.PrintStream.format(String arg0, Object... arg1)

  • System.out.format("format",Object obj) by using this method we can format the text and print

  1. package com.instanceofjava;
  2.  
  3. public class PrintMessage {

  4. public static void main(String[] args){
  5.  
  6.         System.out.format("%s", "James");
  7. }
  8.  
  9. }

Output:

  1. James

3. FileDescriptor:

  • Create PrintStream object by passing FileOutputStream(FileDescriptor.out) object as an argument to the constructor
  1. package com.instanceofjava;
  2.  
  3. public class PrintMessage {

  4. public static void main(String[] args){
  5.  
  6.        PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
              myout.print("i love Java");
  7. }
  8. }

Output:

  1. i love java


4. System.err.print(""):

  • We can use System.err.print("") method to print the message actually its used to print error message
  1. package com.instanceofjava;
  2.  
  3. public static void main(String[] args){
  4.  
  5.        System.err.print("This is custom error message");
  6. }
  7. }

Output:

  1. This is custom error message


5.System.console().writer().println("");

  • System.console() returns null if your application is not run in a terminal System.console() provides methods for reading password without echoing characters

  1. package com.instanceofjava;
  2.  
  3. public static void main(String[] args){
  4.  
  5.        System.err.print("This is custom error message");
  6. }
  7. }

Output:

  1. This is custom error messageException in thread "main" java.lang.NullPointerException
        at PrintMessage.main(PrintMessage.java:24)

How to Iterate ArrayList using Java ListIterator Example

1.Basic Java example program to iterate arraylist elements using list iterator
  1. package com.javaIteratearraylistiterator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class IterateListIteratorArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12. ArrayList<String> arrayList = new ArrayList();
  13.        
  14. //Add elements to Arraylist
  15.  
  16. arrayList.add("A");
  17. arrayList.add("B");
  18. arrayList.add("C");
  19. arrayList.add("D");
  20. arrayList.add("F");
  21. arrayList.add("F");
  22. arrayList.add("G");
  23. arrayList.add("H");
  24. arrayList.add("I");
  25.     
  26.         
  27.  /*
  28. Get a ListIterator object for ArrayList using
  29. istIterator() method.
  30. */
  31.  
  32. ListIterator itr = arrayList.listIterator();
  33.        
  34. /*
  35. Use hasNext() and next() methods of ListIterator to iterate through
  36. the elements in forward direction.
  37. */
  38.  
  39. System.out.println("Iterating through ArrayList elements in forward  direction...");
  40.  
  41. while(itr.hasNext())
  42. System.out.println(itr.next());
  43.      
  44. /*Use hasPrevious() and previous() methods of ListIterator to iterate through
  45. the elements in backward direction.*/
  46.  
  47. System.out.println("Iterating through ArrayList elements in backward   direction...");
  48.         
  49.  
  50. while(itr.hasPrevious())
  51. System.out.println(itr.previous()); 
  52.   
  53. }
  54.  
  55. }
     



Output:

  1. Iterating through ArrayList elements in forward  direction...
  2. A
  3. B
  4. C
  5. D
  6. F
  7. F
  8. G
  9. H
  10. I
  11. Iterating through ArrayList elements in backward   direction...
  12. I
  13. H
  14. G
  15. F
  16. F
  17. D
  18. C
  19. B
  20. A

How to sort arraylist of strings alphabetically java

1.Basic Java example program to sort arraylist of strings

  1. package com.javasortarraylistofobjects;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class SortArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12. ArrayList<String> arrayList = new ArrayList();
  13.        
  14. //Add elements to Arraylist
  15.         
  16. arrayList.add("A"); 
  17. arrayList.add("C");
  18. arrayList.add("D");
  19. arrayList.add("Z");
  20. arrayList.add("F");
  21. arrayList.add("J");
  22. arrayList.add("K");
  23. arrayList.add("M");
  24. arrayList.add("L");
  25. arrayList.add("O");
  26.        
  27.         
  28.  System.out.println("Before sorting ArrayList ...");
  29.  Iterator itr=arrayList.iterator();
  30.         
  31. while (itr.hasNext()) {
  32.  
  33. System.out.println(itr.next());
  34.      
  35. }
  36.  
  37.        
  38.  /*
  39.  To sort an ArrayList object, use Collection.sort method. This is a
  40.   static method. It sorts an ArrayList object's elements into ascending order.
  41. */
  42.   Collections.sort(arrayList);
  43.      
  44.   System.out.println("After sorting ArrayList ...");
  45.        
  46.     
  47.         
  48. Iterator itr1=arrayList.iterator();
  49.         
  50. while (itr1.hasNext()) {

  51. System.out.println(itr1.next());
  52.             
  53. }
  54.     
  55.   
  56. }
  57. }
     




Output:

  1. Before sorting ArrayList ...
  2. A
  3. C
  4. D
  5. Z
  6. F
  7. J
  8. K
  9. M
  10. L
  11. O
  12. After sorting ArrayList ...
  13. A
  14. C
  15. D
  16. F
  17. J
  18. K
  19. L
  20. M
  21. O
  22. Z

Java Program to Sort elements of Java ArrayList Example

1.Basic Java example program to sort arraylist of integers

  1. package com.javasortarraylistofobjects;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class SortArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12.  ArrayList<Integer> arrayList = new ArrayList<Integer>();
  13.        
  14.  //Add elements to Arraylist
  15. arrayList.add(10);
  16. arrayList.add(7);
  17. arrayList.add(11);
  18. arrayList.add(4);
  19. arrayList.add(9);
  20. arrayList.add(6);
  21. arrayList.add(2);
  22. arrayList.add(8);
  23. arrayList.add(5);
  24. arrayList.add(1);
  25.         
  26.         
  27.  System.out.println("Before sorting ArrayList ...");
  28.  Iterator itr=arrayList.iterator();
  29.         
  30. while (itr.hasNext()) {
  31.  
  32. System.out.println(itr.next());
  33.      
  34. }
  35.  
  36.        
  37.  /*
  38.  To sort an ArrayList object, use Collection.sort method. This is a
  39.   static method. It sorts an ArrayList object's elements into ascending order.
  40. */
  41.   Collections.sort(arrayList);
  42.      
  43.   System.out.println("After sorting ArrayList ...");
  44.        
  45.     
  46.         
  47. Iterator itr1=arrayList.iterator();
  48.         
  49. while (itr1.hasNext()) {

  50. System.out.println(itr1.next());
  51.             
  52. }
  53.     
  54.   
  55. }
  56. }
     



Output:

  1. Before sorting ArrayList ...
  2. 10
  3. 7
  4. 11
  5. 4
  6. 9
  7. 6
  8. 2
  9. 8
  10. 5
  11. 1
  12. After sorting ArrayList ...
  13. 1
  14. 2
  15. 4
  16. 5
  17. 6
  18. 7
  19. 8
  20. 9
  21. 10
  22. 11
Select Menu