JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Comparator example with Lambda implementation.


This page gives a simple example of Lambda implementation on Comparator usage. We use Comparator to sort list of elements. Comparator comes with one abstract method called compareTo(). Prior to Java-8, we use anonymous inner class implementation. With Java-8, we can use Lambda to reduce multiple lines of code to single line.

In this example, we have list of Employee objects, we will sort them based on their salary.

package com.java2novice.lambda;

public class Employee {

	private String name;
	private String account;
	private Integer salary;

	public Employee(String name, String account, Integer salary) {
		super();
		this.name = name;
		this.account = account;
		this.salary = salary;
	}

	@Override
	public String toString() {

		return "name: "+ this.name +" | account: "+ this.account +" | salary: "+this.salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public Integer getSalary() {
		return salary;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}
}

Now lets get into Lambda implementation:

package com.java2novice.lambda;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LambdaComparatorEx {

	public static void main(String a[]) {

		List<Employee> empList = new ArrayList<>();
		empList.add(new Employee("Nataraja G", "Accounts", 8000));
		empList.add(new Employee("Nagesh Y", "Admin", 15000));
		empList.add(new Employee("Vasu V", "Security", 2500));
		empList.add(new Employee("Amar", "Entertinment", 8500));

		LambdaComparatorEx.sortInOldWay(empList);
		LambdaComparatorEx.sortInLambdaWay(empList);
	}

	public static void sortInOldWay(List<Employee> empList) {

		Collections.sort(empList, new Comparator<Employee>() {

			@Override
			public int compare(Employee emp1, Employee emp2) {

				return emp1.getSalary().compareTo(emp2.getSalary());
			}
		});

		// print the list using forEach method
		System.out.println("<--- Sorted list with anonomus inner class implementation --->");
		empList.forEach(emp->System.out.println(emp.toString()));
	}

	public static void sortInLambdaWay(List<Employee> empList) {

		Collections.sort(empList, (Employee e1, Employee e2) -> e2.getSalary().compareTo(e1.getSalary()));

		System.out.println("\n\n<--- Sorted list with Lambda - DESC order --->");
		empList.forEach(emp->System.out.println(emp.toString()));

		Collections.sort(empList, (Employee e1, Employee e2) -> e1.getSalary().compareTo(e2.getSalary()));

		System.out.println("\n\n<--- Sorted list with Lambda - ASC order --->");
		empList.forEach(emp->System.out.println(emp.toString()));
	}
}

In the first Lambda implementation, note that we have specified the input type. And in the second Lambda implementation, we ignored the input type declaration, which is fully valid.


Output:
<--- Sorted list with anonomus inner class implementation --->
name: Vasu V | account: Security | salary: 2500
name: Nataraja G | account: Accounts | salary: 8000
name: Amar | account: Entertinment | salary: 8500
name: Nagesh Y | account: Admin | salary: 15000


<--- Sorted list with Lambda - DESC order --->
name: Nagesh Y | account: Admin | salary: 15000
name: Amar | account: Entertinment | salary: 8500
name: Nataraja G | account: Accounts | salary: 8000
name: Vasu V | account: Security | salary: 2500


<--- Sorted list with Lambda - ASC order --->
name: Vasu V | account: Security | salary: 2500
name: Nataraja G | account: Accounts | salary: 8000
name: Amar | account: Entertinment | salary: 8500
name: Nagesh Y | account: Admin | salary: 15000
<< Previous Program | Next Program >>

Java-8 Lambda expression examples

  1. Lambda expression syntax example
  2. Lambda example with single method interface implementation.
  3. Comparator example with Lambda implementation.
  4. How to return lambda as an object?
Knowledge Centre
What is System.out in Java
In System.out, out is an instance of PrintStream. It is a static member variable in System class. This is called standard output stream, connected to console.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.