Java 8 provides a new method forEach() to iterate the elements. It is defined in the Iterable and Stream interface.
It is a default method defined in the Iterable interface. Collection classes that extend the Iterable interface can use the forEach() loop to iterate elements.
It is a default method defined in the Iterable interface. Collection classes that extend the Iterable interface can use the forEach() loop to iterate elements.
Normal for loop with List
Let's use normal for-loop to loop a List.
public static void forEachWithList() {
final List < Person > items = new ArrayList < > ();
items.add(new Person(100, "Ramesh"));
items.add(new Person(100, "A"));
items.add(new Person(100, "B"));
items.add(new Person(100, "C"));
items.add(new Person(100, "D"));
for (final Person item: items) {
System.out.println(item.getName());
}
}
forEach() method with List Example
In Java 8, you can loop a List with forEach + lambda expression or method reference. Please refer to the comments in the above example are self-descriptive.
public static void forEachWithList() {
final List < Person > items = new ArrayList < > ();
items.add(new Person(100, "Ramesh"));
items.add(new Person(100, "A"));
items.add(new Person(100, "B"));
items.add(new Person(100, "C"));
items.add(new Person(100, "D"));
//lambda
items.forEach(item - > System.out.println(item.getName()));
//Output : C
items.forEach(item - > {
if ("C".equals(item)) {
System.out.println(item);
}
});
//method reference
//Output : A,B,C,D,E
items.forEach(System.out::println);
//Stream and filter
//Output : B
items.stream()
.filter(s - > s.getName().equals("Ramesh"))
.forEach(System.out::println);
}
Java ArrayList Source Code Examples
- Sort List of Integers in Ascending and Descending Order Example
- List (ArrayList) Iterator Example
- Create the Immutable ArrayList with List.of() Method Example
- Create Immutable ArrayList with Collections.unmodifiableList() Exmaple
- Java 10 - Copy List into Another List Exmaple
- Java 8 - Copy List into Another List Example
- Java - Copy a List to Another List using Collections.copy() method
- Java - Copy a List to Another List Example
- Java ArrayList spliterator() Method Example
- Java ArrayList sort() Method Example
- Java ArrayList retainAll() Method Example
- Java ArrayList removeIf() Method Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- Java ArrayList lastIndexOf() Method Example
- Java ArrayList isEmpty() Method Example
- Java util ArrayList indexOf() Method Example
- Java ArrayList get() Method Example
- Java ArrayList ensureCapacity() Method Example
- Java ArrayList contains() Method Example
- Java ArrayList clone() Method Example
- Java ArrayList clear() Method Example
- Java ArrayList addAll() Method Example
- Java ArrayList add() Method Example
- Java 8 forEach() List Example
- Add Enum Values to ArrayList Example
- Join List Strings with Commas in Java
- Java Stream filter null values example
- Java ArrayList subList() Example
- Get Index of Elements in ArrayList Example
- Java ArrayList removeIf() Example
- Java ArrayList add(), get() and set() Method Example
- Iterate over ArrayList Using forEach Java
- Iterate over ArrayList using Iterator in Java
- Java ArrayList indexOf() and lastIndexOf() Example
- Search an Element in an ArrayList in Java
- Clear ArrayList in Java Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- How to Iterate over ArrayList in Java
- How to Remove Element from ArrayList in Java
- How to Access Elements of ArrayList in Java
- Create ArrayList from Another ArrayList in Java
- How to Create an ArrayList and Add Elements to It
ArrayList
Java 8
Comments
Post a Comment