List and ArrayList demo using forEach and Comparator in Java 8

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at the demo of List and ArrayList using forEach and Comparator in Java 8.

  • forEach: brand new for loop for looping through the objects in a collection using lambda expressions
  • Comparator: Java 8 has provided a new way of passing a Comparator instance to sort method on a collection using lambda expressions.

We will create a few Person objects and display the details and sort them based on their properties.

Let’s look at the Person class:

 

Here is the Java program:

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Happy Learning :).

Please leave a reply in case of any queries.

 

Leave a Reply

Your email address will not be published. Required fields are marked *