package com.eazybytes;
public class Employee {
private int eid;
private String eName;
private String job;
private double salary;
public Employee(int eid, String eName, String job, double salary) {
this.eid = eid;
this.eName = eName;
this.job = job;
this.salary = salary;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Solution 1-
package com.eazybytes;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeSortExample {
public static void main(String[] args) {
List<Employee> employeeList =
Arrays.asList(
new Employee(1, "nisha patel", "developer", 50000),
new Employee(2, "arjun rao", "designer", 55000),
new Employee(3, "rahul sharma", "manager", 60000),
new Employee(4, "priyanka chopra", "analyst", 52000),
new Employee(5, "rohan khanna", "tester", 48000),
new Employee(6, null, "developer", 50500),
new Employee(7, "amit agarwal", "designer", 55500),
new Employee(8, "meera nair", "manager", 60500),
new Employee(9, "vikas gupta", "analyst", 52500),
new Employee(10, "neha jain", "tester", 48500));
List<Employee> nameList =
employeeList.stream().map(employee -> {
if (employee.geteName() != null) {
employee.seteName(employee.geteName().toUpperCase());
}
return employee;
}).collect(Collectors.toList());
nameList.forEach(employee -> System.out.println("Employee Name:" + employee.geteName() + ", Salary: " + employee.getSalary()));
}
}
Output :
Employee Name:NISHA PATEL, Salary: 50000.0
Employee Name:ARJUN RAO, Salary: 55000.0
Employee Name:RAHUL SHARMA, Salary: 60000.0
Employee Name:PRIYANKA CHOPRA, Salary: 52000.0
Employee Name:ROHAN KHANNA, Salary: 48000.0
Employee Name:null, Salary: 50500.0
Employee Name:AMIT AGARWAL, Salary: 55500.0
Employee Name:MEERA NAIR, Salary: 60500.0
Employee Name:VIKAS GUPTA, Salary: 52500.0
Employee Name:NEHA JAIN, Salary: 48500.0
Solution 2- Improved version
package com.eazybytes;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class EmployeeSortExample {
public static void main(String[] args) {
List<Employee> employeeList =
Arrays.asList(new Employee(1, "nisha patel", "developer", 50000),
new Employee(2, "arjun rao", "designer", 55000),
new Employee(3, "rahul sharma", "manager", 60000),
new Employee(4, "priyanka chopra", "analyst", 52000),
new Employee(5, "rohan khanna", "tester", 48000),
new Employee(6, null, "developer", 50500),
new Employee(7, "amit agarwal", "designer", 55500),
new Employee(8, "meera nair", "manager", 60500),
new Employee(9, "vikas gupta", "analyst", 52500),
new Employee(10, "neha jain", "tester", 48500));
List<Employee> nameList =
employeeList.stream().map(employee -> {
Optional.ofNullable(employee.geteName()).ifPresent(name -> {
employee.seteName(name.toUpperCase());
});
return employee;
}).collect(Collectors.toList());
nameList.forEach(employee -> System.out.println("Employee Name:" + employee.geteName() + ", Salary: " + employee.getSalary()));
}
}
Output :
Employee Name:NISHA PATEL, Salary: 50000.0
Employee Name:ARJUN RAO, Salary: 55000.0
Employee Name:RAHUL SHARMA, Salary: 60000.0
Employee Name:PRIYANKA CHOPRA, Salary: 52000.0
Employee Name:ROHAN KHANNA, Salary: 48000.0
Employee Name:null, Salary: 50500.0
Employee Name:AMIT AGARWAL, Salary: 55500.0
Employee Name:MEERA NAIR, Salary: 60500.0
Employee Name:VIKAS GUPTA, Salary: 52500.0
Employee Name:NEHA JAIN, Salary: 48500.0
Key changes:
Optional.ofNullable
: This safely handles null
values by only attempting to modify the name if it’s non-null.
- Stream operations: The code is now more concise and still maintains readability, handling
null
gracefully without explicit checks.