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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.sample.collections; public class Person { private String firstname; private String lastname; private int persionid; private double salary; public Person(String firstname, String lastname, int personid, double salary) { this.firstname = firstname; this.lastname = lastname; this.persionid = personid; this.salary = salary; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getPersionid() { return persionid; } public void setPersionid(int persionid) { this.persionid = persionid; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } |
Here is the Java program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
package com.sample.collections.listdemo; import java.util.List; import java.util.ArrayList; import java.util.Comparator; import com.sample.collections.Person; public class ListAndArrayListDemo { public static void main(String[] args) { //initialize an ArrayList List<Person> persons = new ArrayList<Person>(); //create Person instances Person person1 = new Person("Adam", "John", 101, 3200.0); Person person2 = new Person("Chris", "Wade", 102, 4990.0); Person person3 = new Person("Joe", "Andy", 103, 4100.0); Person person4 = new Person("Victor", "High", 104, 3700.0); Person person5 = new Person("Adam", "Pal", 105, 5000.0); Person person6 = new Person("Christofer", "James", 106, 4100.0); Person person7 = new Person("Jack", "Jill", 107, 4600.0); Person person8 = new Person("Chris", "Patt", 108, 3900.0); Person person9 = new Person("Brian", "Dune", 109, 4800.0); Person person10 = new Person("Willi", "Kates", 110, 3750.0); //add person instances to ArrayList persons.add(person1); persons.add(person2); persons.add(person3); persons.add(person4); persons.add(person5); persons.add(person6); persons.add(person7); persons.add(person8); System.out.println("Person details: "); System.out.println(); //Java 8 forEach for looping through the List of objects persons.forEach(p -> { System.out.println(" Name: " + p.getLastname() + ", " + p.getFirstname() + " @@ Person Id: " + p.getPersionid() + " @@ Salary: " + p.getSalary()); }); //add person instances at different index persons.add(4, person10); persons.add(5, person9); System.out.println(); System.out.println(); System.out.println("Person details after adding objects at different index: "); System.out.println(); //Java 8 forEach for looping through the List of objects persons.forEach(p -> { System.out.println(" Name: " + p.getLastname() + ", " + p.getFirstname() + " @@ Person Id: " + p.getPersionid() + " @@ Salary: " + p.getSalary()); }); //sort the objects in the list based on first name and last name //Java 8 has provided a new way of initializing Comparator // using static methods - comparing(), thenComaparing() persons.sort(Comparator.comparing((Person p) -> p.getFirstname()) .thenComparing(p -> p.getLastname()) ); System.out.println(); System.out.println(); System.out.println("Person details after sorting based on First name and Last name: "); System.out.println(); //Java 8 forEach for looping through the List of objects persons.forEach(p -> { System.out.println(" Name: " + p.getLastname() + ", " + p.getFirstname() + " @@ Person Id: " + p.getPersionid() + " @@ Salary: " + p.getSalary()); }); //sort the objects in the list based on salary in descending order //We can sort the objects in descending order by calling static method - "reversed()" persons.sort(Comparator.comparing((Person p) -> p.getSalary()).reversed()); System.out.println(); System.out.println(); System.out.println("Person details after sorting based on First name and Last name: "); System.out.println(); //Java 8 forEach for looping through the List of objects persons.forEach(p -> { System.out.println(" Name: " + p.getLastname() + ", " + p.getFirstname() + " @@ Person Id: " + p.getPersionid() + " @@ Salary: " + p.getSalary()); }); } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.