We already studied a lot of good Java Collection's interview questions in previous posts. But one thing that is very important while talking about collections is "Iterating the collection", because whenever we use collection in any application we always iterate it. We have a lot of ways to iterate over a collection and in this post I am going to explain the different ways of iterating the collection.
The main ways of iterating over a collection are -
Iterating the collection with Java's Iterator interface - Iterator interface is present in java.util package. Different collection classes have specific implementation for iterator. All classes which implements Iterable interface can be iterated using Iterator interface. Iterators allow the caller to remove elements from the underlying collection during the iteration.
Code sample to use -
// Iterating with Java Iterator
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
Iterator<String> collectionIterator = collection.iterator();
while (collectionIterator.hasNext()) {
System.out.println(collectionIterator.next());
}
Iterating the collection with Java 5 enhanced for-each loop - In Java 5 new enhanced loop was introduced to iterate over the collections and arrays. For-each loop was introduced to mainly increase the code readability and minimize the chances of bugs.
Code sample to use -
// Iterating with Java 5 enhanced for-each loop
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
for (String elemnet : collection) {
System.out.println(elemnet);
}
Iterating the collection with Java 8 Iterable's forEach method & Consumer interface - This method was introduced in Java 8. Consumer interface is used with Java 8 Iterables forEach to iterates the collections.
java.util.functional.Consumer is a functional interface, which accepts one parameter and return no results.
Code sample to use -
// Iterating with Java 8 Iterable's foreach method & Consumer interface using Lambda exp.
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
collection.forEach(s -> System.out.println(s));
OR
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
Consumer<String> style = (String s) -> System.out.println(s);
collection.forEach(style);
So, That's All.
Below is the complete class that you can copy and run directly.
package javaprinciples.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class IterateCollection {
public static void main(String[] args) {
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
// Iterating with simple for loop
System.out.println("Iterating with simple for loop ..............");
ArrayList<String> arrayList = new ArrayList<String>(collection);
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
// Iterating with Java Iterator
System.out.println("Iterating with Java Iterator ...........");
Iterator<String> collectionIterator = collection.iterator();
while (collectionIterator.hasNext()) {
System.out.println(collectionIterator.next());
}
// Iterating with Java 5 enhanced for-each loop
System.out.println("Iterating with Java 5 enhanced for-each loop...........");
for (String elemnet : collection) {
System.out.println(elemnet);
}
// Iterating with Java 8 Iterable's foreach method & Consumer interface
// using Lambda exp.
System.out.println("Iterating with Java 8 Iterable's foreach method...........");
collection.forEach(s -> System.out.println(s));
}
}
If you have any doubts please put in comments, I will try to resolve ASAP.
The main ways of iterating over a collection are -
- Iterating the collection with a simple loop.
- Iterating the collection with Java's Iterator interface.
- Iterating the collection with Java 5 enhanced for-each loop
- Iterating the collection with Java 8 Iterable's foreach method & Consumer interface.
Iterating the collection with a simple loop - In this technique we use a simple loop to iterate the collection, but with this ways we can only iterate the indexed collections only. Code sample to use -
// Iterating with simple for loop
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
ArrayList<String> arrayList = new ArrayList<String>(collection);
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
Iterating the collection with Java's Iterator interface - Iterator interface is present in java.util package. Different collection classes have specific implementation for iterator. All classes which implements Iterable interface can be iterated using Iterator interface. Iterators allow the caller to remove elements from the underlying collection during the iteration.
Code sample to use -
// Iterating with Java Iterator
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
Iterator<String> collectionIterator = collection.iterator();
while (collectionIterator.hasNext()) {
System.out.println(collectionIterator.next());
}
Iterating the collection with Java 5 enhanced for-each loop - In Java 5 new enhanced loop was introduced to iterate over the collections and arrays. For-each loop was introduced to mainly increase the code readability and minimize the chances of bugs.
Code sample to use -
// Iterating with Java 5 enhanced for-each loop
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
for (String elemnet : collection) {
System.out.println(elemnet);
}
Iterating the collection with Java 8 Iterable's forEach method & Consumer interface - This method was introduced in Java 8. Consumer interface is used with Java 8 Iterables forEach to iterates the collections.
java.util.functional.Consumer is a functional interface, which accepts one parameter and return no results.
Code sample to use -
// Iterating with Java 8 Iterable's foreach method & Consumer interface using Lambda exp.
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
collection.forEach(s -> System.out.println(s));
OR
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
Consumer<String> style = (String s) -> System.out.println(s);
collection.forEach(style);
So, That's All.
Below is the complete class that you can copy and run directly.
package javaprinciples.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class IterateCollection {
public static void main(String[] args) {
Collection<String> collection = Arrays.asList("Vijendra", "Yadav", "javaprinciples", "blog");
// Iterating with simple for loop
System.out.println("Iterating with simple for loop ..............");
ArrayList<String> arrayList = new ArrayList<String>(collection);
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
// Iterating with Java Iterator
System.out.println("Iterating with Java Iterator ...........");
Iterator<String> collectionIterator = collection.iterator();
while (collectionIterator.hasNext()) {
System.out.println(collectionIterator.next());
}
// Iterating with Java 5 enhanced for-each loop
System.out.println("Iterating with Java 5 enhanced for-each loop...........");
for (String elemnet : collection) {
System.out.println(elemnet);
}
// Iterating with Java 8 Iterable's foreach method & Consumer interface
// using Lambda exp.
System.out.println("Iterating with Java 8 Iterable's foreach method...........");
collection.forEach(s -> System.out.println(s));
}
}
If you have any doubts please put in comments, I will try to resolve ASAP.
Check below links also-
Top 20 Collection Framework Interview Questions
Core Java Interview Questions - OOps and Basics
Core Java Interview Questions - String
Servlet Interview Questions
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java
Top 20 Collection Framework Interview Questions
Core Java Interview Questions - OOps and Basics
Core Java Interview Questions - String
Servlet Interview Questions
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java
No comments:
Post a Comment