0% found this document useful (0 votes)
7 views4 pages

Lambda Expressions for Employee Sorting

Uploaded by

Ishant singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Lambda Expressions for Employee Sorting

Uploaded by

Ishant singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEPARTMENT OF

Experiment 6
Student Name: Jeeya UID: 23BCS12790
Branch: CSE Section/Group: 606/B
Semester: 5th Date of Performance: 14/08/25
Subject Name: PBLJ Subject Code: 23CSH-304

Aim: Write a program to sort a list of Employee objects (name, age,


salary) using lambda expressions.

Objective: To understand how lambda expressions simplify sorting


logic and enhance code readability.

Implementation/Code:

[Link]

import [Link].*;

class Employee {
private String name;
private int age;
private double salary;

public Employee(String name, int age, double salary) {


DEPARTMENT OF

[Link] = name;
[Link] = age;
[Link] = salary;
}

public String getName() { return name; }


public int getAge() { return age; }
public double getSalary() { return salary; }

@Override
public String toString() {
return name + " - " + age + " - " + salary;
}
}

public class LambdaSortingExample {


public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
[Link](new Employee("John", 30, 50000));
[Link](new Employee("Alice", 25, 60000));
[Link](new Employee("Bob", 28, 75000));
[Link](new Employee("David", 35, 55000));

[Link]("Sorted by Salary:");
[Link]((e1, e2) -> [Link]([Link](),
[Link]()));
[Link]([Link]::println);

[Link]("\nSorted by Age:");
[Link]((e1, e2) -> [Link]([Link](),
[Link]()));
[Link]([Link]::println);

[Link]("\nSorted by Name:");
DEPARTMENT OF

[Link]((e1, e2) -> [Link]().compareTo([Link]()));


[Link]([Link]::println);
}
}

Output:
DEPARTMENT OF

Learning Outcome:

1. Application of Lambda Expressions in Sorting – Students will


learn how to use lambda syntax to define concise comparator logic
without writing separate classes or methods.
2. Enhanced Understanding of Collections API – Students will gain
practical experience with [Link]() and [Link]() to organize
objects dynamically.
3. Working with Object-Oriented Data Structures – By defining an
Employee class and encapsulating attributes, students will practice
sorting user-defined objects instead of simple primitives.
4. Improved Code Readability and Maintainability – Students will
understand how lambda expressions reduce boilerplate code, making
sorting logic easier to read and maintain.
5. Exploring Multiple Sorting Criteria – Students will learn to sort
by different fields (name, age, salary), improving problem-solving
skills and preparing them for handling real-world datasets.

You might also like