Getting sorted Employee HashMap from provided Employee HashMap

 


To return a HashMap sorted by the employee's salary using Java 8, you can follow these steps:
1. Convert the HashMap to a Stream of Map. Entry objects using the entrySet() method of the HashMap class.
2.    Sort the Stream of Map. Entry objects based on the salary of the employee using the comparing the () method of the Comparator class.
3. Collect the sorted Stream of Map. Entry objects to a LinkedHashMap using the collect() method of the Stream interface.

import java.util.*;
import java.util.stream.*;

public class SortHashMapBySalary {

    public static void main(String[] args) {

        // Create a HashMap to store employees
        HashMap<Integer, Employee> employeeMap = new HashMap<>();
        employeeMap.put(1, new Employee(1, "Manoj", 50000));
        employeeMap.put(2, new Employee(2, "Saroj", 60000));
        employeeMap.put(3, new Employee(3, "Prdeep", 55000));
        employeeMap.put(4, new Employee(4, "Lalit", 65000));

        // Sort the HashMap by salary using Java 8
        LinkedHashMap<Integer, Employee> sortedBySalary = employeeMap.entrySet()
            .stream()
            .sorted(Map.Entry.comparingByValue(Comparator.comparing(Employee::getSalary)))
            .collect(Collectors.toMap(
                Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

        // Print the sorted HashMap by salary
        System.out.println("Sorted Employee HashMap by Salary: ");
        System.out.println(sortedBySalary);
    }
}

class Employee {
    private int id;
    private String name;
    private int salary;

    public Employee(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }

    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}




In this example code, we first create a HashMap called employeeMap to store employees. We add four Employee objects to the map with IDs 1, 2, 3, and 4.
Next, we sort the HashMap by salary using Java 8. We use the entrySet() method to get a Stream of Map.Entry objects from the HashMap. We then sort the Stream of Map.Entry objects based on the salary of the employee using the comparing () method of the Comparator class. We then collect the sorted Stream of Map. Entry objects to a LinkedHashMap using the collect() method of the Stream interface.
Finally, we print the sorted HashMap by salary using the println() method of the System.out the object. Note that the output is in the order of increasing salary, with the employee with the lowest salary appearing first.

Getting sorted Employee HashMap from provided Employee HashMap   Getting sorted Employee HashMap  from  provided Employee  HashMap Reviewed by Mukesh Jha on 11:03 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.