0% found this document useful (0 votes)
8 views3 pages

pblj6 Removed

The document outlines an experiment for a Computer Science and Engineering course where students write a Java program to sort a list of Employee objects by name, age, and salary using lambda expressions. It includes the program's aim, objectives, implementation code, and expected learning outcomes. The experiment emphasizes understanding lambda expressions, custom sorting with the Comparator interface, and using method references for cleaner code.

Uploaded by

milantchgropse
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)
8 views3 pages

pblj6 Removed

The document outlines an experiment for a Computer Science and Engineering course where students write a Java program to sort a list of Employee objects by name, age, and salary using lambda expressions. It includes the program's aim, objectives, implementation code, and expected learning outcomes. The experiment emphasizes understanding lambda expressions, custom sorting with the Comparator interface, and using method references for cleaner code.

Uploaded by

milantchgropse
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
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 6.1
Student Name: Nikita UID:22BCS16902
Branch: CSE Section/Group:22BCS_IOT_644 ‘A’
Semester: 6 Date of Performance:03/03/25
Subject Name: Project Based Learning Subject Code: 22CSH-352
In Java with Lab

Easy:

1. Aim: Write a program to sort a list of Employee objects (name, age, salary) using
lambda expressions.

2. Objective:
 To demonstrate sorting of a list of Employee objects using lambda expressions in
Java.
 To implement custom sorting based on name, age, and salary using the Comparator
interface.

3. Implementation/Code:
import java.util.*;

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

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


this.name = name;
this.age = age;
this.salary = salary;
}

public void display() {


System.out.println("Name: " + name + ", Age: " + age + ", Salary: " + salary);
}
}
public class EmployeeSort {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public static void main(String[] args) {


List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Alice", 30, 50000));
employees.add(new Employee("Bob", 25, 60000));
employees.add(new Employee("Charlie", 35, 55000));

System.out.println("Before Sorting:");
employees.forEach(Employee::display);

employees.sort((e1, e2) -> e1.name.compareTo(e2.name));


System.out.println("\nSorted by Name:");
employees.forEach(Employee::display);

employees.sort((e1, e2) -> Integer.compare(e1.age, e2.age));


System.out.println("\nSorted by Age:");
employees.forEach(Employee::display);

employees.sort((e1, e2) -> Double.compare(e1.salary, e2.salary));


System.out.println("\nSorted by Salary:");
employees.forEach(Employee::display);
}
}

4. OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Learning Outcomes:
 Understand the use of lambda expressions for sorting in Java.
 Learn how to implement custom sorting using Comparator with different data
types.
 Gain hands-on experience with the sort() method in List.
 Explore method references for clean and readable code.
 Develop efficient sorting techniques without writing separate comparator classes.

You might also like