Add items to list in for loop java

Add items to list in for loop java

How to iterate through Java List? This tutorial demonstrates the use of ArrayList, Iterator and a List.

There are 7 ways you can iterate through List.

  1. Simple For loop
  2. Enhanced For loop
  3. Iterator
  4. ListIterator
  5. While loop
  6. Iterable.forEach() util
  7. Stream.forEach() util

Java Example:

You need JDK 13 to run below program as point-5 above uses stream() util.

void java.util.stream.Stream.forEach(Consumer action) performs an action for each element of this stream.

CrunchifyIterateThroughList.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package crunchify.com.tutorials;
import java.util.*;
/**
* @author Crunchify.com
* How to iterate through Java List? Seven (7) ways to Iterate Through Loop in Java.
* 1. Simple For loop
* 2. Enhanced For loop
* 3. Iterator
* 4. ListIterator
* 5. While loop
* 6. Iterable.forEach() util
* 7. Stream.forEach() util
*/
public class CrunchifyIterateThroughList {
public static void main(String[] argv) {
// create list
List crunchifyList = new ArrayList();
// add 4 different values to list
crunchifyList.add("Facebook");
crunchifyList.add("Paypal");
crunchifyList.add("Google");
crunchifyList.add("Yahoo");
// Other way to define list is - we will not use this list :)
List crunchifyListNew = Arrays.asList("Facebook", "Paypal", "Google", "Yahoo");
// Simple For loop
System.out.println("==============> 1. Simple For loop Example.");
for (int i = 0; i < crunchifyList.size(); i++) {
System.out.println(crunchifyList.get(i));
}
// New Enhanced For loop
System.out.println("\n==============> 2. New Enhanced For loop Example..");
for (String temp : crunchifyList) {
System.out.println(temp);
}
// Iterator - Returns an iterator over the elements in this list in proper sequence.
System.out.println("\n==============> 3. Iterator Example...");
Iterator crunchifyIterator = crunchifyList.iterator();
while (crunchifyIterator.hasNext()) {
System.out.println(crunchifyIterator.next());
}
// ListIterator - traverse a list of elements in either forward or backward order
// An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration,
// and obtain the iterator's current position in the list.
System.out.println("\n==============> 4. ListIterator Example...");
ListIterator crunchifyListIterator = crunchifyList.listIterator();
while (crunchifyListIterator.hasNext()) {
System.out.println(crunchifyListIterator.next());
}
// while loop
System.out.println("\n==============> 5. While Loop Example....");
int i = 0;
while (i < crunchifyList.size()) {
System.out.println(crunchifyList.get(i));
i++;
}
// Iterable.forEach() util: Returns a sequential Stream with this collection as its source
System.out.println("\n==============> 6. Iterable.forEach() Example....");
crunchifyList.forEach((temp) -> {
System.out.println(temp);
});
// collection Stream.forEach() util: Returns a sequential Stream with this collection as its source
System.out.println("\n==============> 7. Stream.forEach() Example....");
crunchifyList.stream().forEach((crunchifyTemp) -> System.out.println(crunchifyTemp));
}
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
==============> 1. Simple For loop Example.
Facebook
Paypal
Google
Yahoo
==============> 2. New Enhanced For loop Example..
Facebook
Paypal
Google
Yahoo
==============> 3. Iterator Example...
Facebook
Paypal
Google
Yahoo
==============> 4. ListIterator Example...
Facebook
Paypal
Google
Yahoo
==============> 5. While Loop Example....
Facebook
Paypal
Google
Yahoo
==============> 6. Iterable.forEach() Example....
Facebook
Paypal
Google
Yahoo
==============> 7. Stream.forEach() Example....
Facebook
Paypal
Google
Yahoo
Process finished with exit code 0

Join the Discussion

If you liked this article, then please share it on social media. Still have any questions about an article, leave us a comment.

Share:
  1. PayPal Java SDK Complete Example How to Invoke PayPal Authorization REST API using Java Client?
  2. In Java How to Shuffle, Reverse, Copy, Rotate and Swap List using Collection APIs?
  3. How to Read a File line by line using Java Stream Files.lines() and Files.newBufferedReader() Utility APIs
  4. In Java8 How to Convert Array to Stream using Arrays.stream() and Stream.of() Operations
  5. How to Iterate Through Map and List in Java? Example attached (Total 5 Different Ways)
  6. How to Reverse Singly Linked List in Java? (Also Addition, Iterate Methods)