![]() ![]() If you will run above example code, you will get output like this: High Nums parallel = 91 High Nums parallel = 96 High Nums parallel = 93 High Nums parallel = 98 High Nums parallel = 94 High Nums parallel = 95 High Nums parallel = 97 High Nums parallel = 92 High Nums parallel = 99 High Nums sequential = 91 High Nums sequential = 92 High Nums sequential = 93 High Nums sequential = 94 High Nums sequential = 95 High Nums sequential = 96 High Nums sequential = 97 High Nums sequential = 98 High Nums sequential = 99 ![]() Integer public class Java8ForEachExample Let’s see forEach usage with a simple example. The forEach method takes object as an argument, so it helps in having our business logic at a separate location that we can reuse. Java 8 has introduced forEach method in interface so that while writing code we focus on business logic. We might get ConcurrentModificationException if the iterator is not used properly. Whenever we need to traverse through a Collection, we need to create an Iterator whose whole purpose is to iterate over, and then we have business logic in a loop for each of the elements in the Collection. I will provide some code snippets for better understanding the features in a simple way. Let’s have a brief look on these Java 8 features.
0 Comments
Leave a Reply. |