🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
This tutorial will demonstrate how to create a Java program that sorts employees' salaries in descending order. Sorting salaries this way can help identify top earners and allocate resources accordingly.
Key Points
- Use of a custom Employee class with a salary attribute.
- Application of Java's Stream API for efficient data manipulation.
- Sorting a list of objects based on a salary field in descending order.
2. Program Steps
1. Define the Employee class with necessary attributes, including salary.
2. Create a list of Employee instances.
3. Use Java's Stream API to sort the salaries in descending order.
4. Print the sorted list of employees.
3. Code Program
// File: Employee.java
public class Employee {
private int id;
private String name;
private int age;
private long salary;
private String gender;
private String deptName;
private String city;
private int yearOfJoining;
public Employee(int id, String name, int age, long salary, String gender, String deptName, String city, int yearOfJoining) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.gender = gender;
this.deptName = deptName;
this.city = city;
this.yearOfJoining = yearOfJoining;
}
public long getSalary() {
return salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", gender='" + gender + '\'' +
", deptName='" + deptName + '\'' +
", city='" + city + '\'' +
", yearOfJoining=" + yearOfJoining +
'}';
}
}
// File: SortSalariesDescending.java
import java.util.*;
import java.util.stream.Collectors;
public class SortSalariesDescending {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai", 1995),
new Employee(2, "Rahul", 25, 130000, "M", "Engineering", "Bangalore", 2000),
new Employee(3, "Vishal", 34, 110000, "M", "Engineering", "Mumbai", 1998),
new Employee(4, "Lakshmi", 28, 150000, "F", "HR", "Bangalore", 1992),
new Employee(5, "Priya", 24, 90000, "F", "Marketing", "Delhi", 2005)
);
List<Employee> sortedEmployees = employees.stream()
.sorted(Comparator.comparingLong(Employee::getSalary).reversed())
.collect(Collectors.toList());
sortedEmployees.forEach(System.out::println);
}
}
Output:
Employee{id=4, name='Lakshmi', age=28, salary=150000, gender='F', deptName='HR', city='Bangalore', yearOfJoining=1992}
Employee{id=2, name='Rahul', age=25, salary=130000, gender='M', deptName='Engineering', city='Bangalore', yearOfJoining=2000}
Employee{id=3, name='Vishal', age=34, salary=110000, gender='M', deptName='Engineering', city='Mumbai', yearOfJoining=1998}
Employee{id=1, name='Aditi', age=30, salary=100000, gender='F', deptName='HR', city='Mumbai', yearOfJoining=1995}
Employee{id=5, name='Priya', age=24, salary=90000, gender='F', deptName='Marketing', city='Delhi', yearOfJoining=2005}
Explanation:
1. The Employee class is defined with necessary fields including salary, which is critical for sorting.
2. A list of Employee objects is created in the SortSalariesDescending main method, and Java Streams are used to sort these objects.
3. The Comparator.comparingLong(Employee::getSalary).reversed() is utilized to sort the salaries in descending order.
4. The sorted list is collected into a new list and printed, showing employees from the highest to the lowest salary.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment