56 / 100

Iterating in Java refers to accessing and processing each element of a collection or array. Iteration is a fundamental concept in programming, and it is used to perform repetitive tasks efficiently.

In Java, several ways exist to iterate over a collection or array, including for loops, enhanced for loops, iterators, and streams.

A for loop is a traditional loop that executes a block of code repeatedly based on a condition. The loop counter is initialized, tested against a condition, and incremented or decremented in each iteration. Here’s an example of iterating over an array using a ‘for loop’:

 


int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

 

In this example, we use a ‘for loop’ to iterate over an array of integers. The loop counter i is initialized to 0, tested against the length of the array using numbers.length and incremented in each iteration. The loop body prints each element of the array.

An enhanced for loop, also called a ‘for-each loop,’ is a simplified version of a ‘for loop’ designed for iterating over collections and arrays. Here’s an example of using an enhanced for loop to iterate over a list of strings:

 


List names = List.of("Alice", "Bob", "Charlie");
for (String name : names) {
    System.out.println(name);
} 

 

In this example, we use an enhanced for loop to iterate over a list of strings. The loop variable name is initialized to the value of each element of the list in turn, and the loop body prints each element.

An iterator is an object that provides a way to access the elements of a collection sequentially without exposing its underlying representation. Iterators are used when you want to perform operations on a collection, such as removing elements or iterating in a specific order. Here’s an example of using an iterator to iterate over a set of integers:

 


Set numbers = Set.of(1, 2, 3, 4, 5);
Iterator iterator = numbers.iterator();
while (iterator.hasNext()) {
    Integer number = iterator.next();
    System.out.println(number);
}

 

In this example, we use an iterator to iterate over a set of integers. The iterator is obtained from the set using numbers.iterator(), and the hasNext() method is used to test if there are more elements to process. The next() method returns the next element in the set, and the loop body prints each element.

Streams are a new feature introduced in Java 8 that provides a powerful way to process collections and arrays. Streams are designed to operate on collections in a functional style and can filter, map, and reduce collections efficiently. Here’s an example of using a stream to iterate over an array of integers and print their sum:

 


int[] numbers = {1, 2, 3, 4, 5};
int sum = Arrays.stream(numbers).sum();
System.out.println(sum);

 

In this example, we use a stream to iterate over an array of integers and compute their sum using the sum() method. The Arrays.stream(numbers) method creates a stream of integers from the array, and the sum() method computes the sum of the elements.

In summary, iteration is a fundamental concept in Java programming, and it provides a way to access and process each element of a collection or array efficiently. Java provides several ways to iterate over collections and arrays, including for loops, enhanced for loops, iterators, and streams.