
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to iterate List Using Java Stream API?
The List interface is a part of the Java Collection framework and it extends the Collection interface. A list stores a sequence of elements and these elements are searable and accessible using indexes. ArrayList is the most popular implementation of the List interface. A list provides quite precise control over where an element is to be inserted in the List.
List interface provides a stream() method which gives a stream to iterate using forEach method. In forEach method, we can use the lambda expression to iterate over all elements. The following code snippet shows the usage of streams to iterate over the list.
list.stream().forEach(i -> {System.out.print(i + " ");});
In this article, we're discussing use of streams to iterate a list in given examples.
Example 1
Following is the example showing the use of stream API to iterate the list of numbers −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); list.stream().forEach(i -> {System.out.print(i + " ");}); } }
Output
This will produce the following result −
1 2 3 4 5
Example 2
Following is the example showing the use of stream API to iterate the list of string −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); list.stream().forEach(i -> {System.out.print(i + " ");}); } }
Output
This will produce the following result −
A B C
- Related Articles
- Importance of iterate() method of Stream API in Java 9?\n
- How to iterate a Java List using Iterator?
- How to iterate List using Iterator in Java?
- How to iterate List Using Streams in Java?
- How to iterate a Java List using For Loop?
- How to sort a collection by using Stream API with lambdas in Java?
- How to iterate a Java List using For-Each Loop?
- How to iterate a List using for Loop in Java?
- How to iterate through Java List?
- How to iterate a Java List?
- How to iterate a List using for-Each Loop in Java?
- How to iterate over a Java list?
- How to iterate a list in Java?
- How to use the collect() method in Stream API in Java 9?
- How to iterate over a list in Java?
