Java Reference
In-Depth Information
Problem
Your application contains a list of objects and you want to iterate over that list and print
out a field for each object.
Solution
Utilize the new Java Streams API to iterate over the list of data and perform a task for
each element. In the following lines of code, a list is populated with sample data. It is
then iterated by generating a stream, and then each element is printed to the command-
line using a lambda expression.
List<String> myList = new ArrayList();
// Populate the list
for(int x = 0; x <= 10; x++){
myList.add("Test " + x);
}
// Print each element within the list
myList.stream().forEach((value)->{
System.out.println(value);
});
Results:
Test 0
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Search WWH ::




Custom Search