首页 > 解决方案 > 如何从员工列表中打印特定月份加入的员工列表?

问题描述

如何从员工列表中打印特定月份加入的员工列表?

嗨,我正在尝试打印假设“六月”月份加入的员工名单?下面是我的代码,Pojo 类:-

import java.time.LocalDate;

public class Employee {

    private String name;
    private String empID;
    private Designation designation;
    private LocalDate dateOfJoining;
    private int monthlySalary;

    public Employee(String name, String empID, Designation designation, LocalDate dateOfJoining, int monthlySalary) {
        super();
        this.name = name;
        this.empID = empID;
        this.designation = designation;
        this.dateOfJoining = dateOfJoining;
        this.monthlySalary = monthlySalary;
    }

    public Employee() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmpID() {
        return empID;
    }

    public void setEmpID(String empID) {
        this.empID = empID;
    }

    public Designation getDesignation() {
        return designation;
    }

    public void setDesignation(Designation designation) {
        this.designation = designation;
    }

    public LocalDate getDOJ() {
        return dateOfJoining;
    }

    public void setDOJ(LocalDate dOJ) {
        dateOfJoining = dOJ;
    }

    public int getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(int monthlySalary) {
        this.monthlySalary = monthlySalary;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((dateOfJoining == null) ? 0 : dateOfJoining.hashCode());
        result = prime * result + ((designation == null) ? 0 : designation.hashCode());
        result = prime * result + ((empID == null) ? 0 : empID.hashCode());
        result = prime * result + monthlySalary;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (dateOfJoining == null) {
            if (other.dateOfJoining != null)
                return false;
        } else if (!dateOfJoining.equals(other.dateOfJoining))
            return false;
        if (designation == null) {
            if (other.designation != null)
                return false;
        } else if (!designation.equals(other.designation))
            return false;
        if (empID == null) {
            if (other.empID != null)
                return false;
        } else if (!empID.equals(other.empID))
            return false;
        if (monthlySalary != other.monthlySalary)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", empID=" + empID + ", designation=" + designation + ", DOJ=" + dateOfJoining
                + ", monthlySalary=" + monthlySalary + "]";
    }

}

我为加入详细信息的日期创建了一个单独的类。

import java.time.LocalDate;

public class JoiningDate {

    LocalDate date1 = LocalDate.of(2019, 06, 15);
    LocalDate date2 = LocalDate.of(2009, 06, 06);
    LocalDate date3 = LocalDate.of(2007, 05, 10);
    LocalDate date4 = LocalDate.of(2000, 05, 30);
    LocalDate date5 = LocalDate.of(1998, 07, 31);
    LocalDate date6 = LocalDate.of(1995, 12, 12);

}

现在我正在尝试使用 Hashmap 打印 6 月份加入的员工的姓名,以 doj 为键,名称为值。但我无法继续。有人可以帮忙吗?以下是我的代码: -

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Employecomparable {

    public static void main(String[] args) {
        Employee emp = new Employee();
        JoiningDate jd = new JoiningDate();
        List<Employee> listofemployee = new ArrayList<>();

        listofemployee.add(new Employee("Pink", "12345", Designation.ASE, jd.date1, 20000));
        listofemployee.add(new Employee("Red", "24680", Designation.SE, jd.date2, 30000));
        listofemployee.add(new Employee("Blue", "13570", Designation.SSE, jd.date3, 40000));
        listofemployee.add(new Employee("Orange", "13690", Designation.TL, jd.date4, 60000));
        listofemployee.add(new Employee("Green", "10909", Designation.AM, jd.date5, 800000));
        listofemployee.add(new Employee("Yellow", "89076", Designation.M, jd.date6, 2000));

LocalDate today = LocalDate.now();
        System.out.println(today);
        Period time1 = Period.between(jd.date1, today);
        Period time2 = Period.between(jd.date2, today);
        Period time3 = Period.between(jd.date3, today);
        Period time4 = Period.between(jd.date4, today);
        Period time5 = Period.between(jd.date5, today);
        Period time6 = Period.between(jd.date6, today);

        Map<LocalDate,String> hashmap =new HashMap<>();

        for(Employee employee: listofemployee) {
            LocalDate key = employee.getDOJ();
            String value = employee.getName();
            if (hashmap.containsKey(key)) {
                ArrayList<Employee> list = new ArrayList<Employee>();
                list.add(employee);
                hashmap.put(key, value);
        }
            }
        System.out.println(hashmap);

标签: javahashmap

解决方案


如果您只想打印 6 月加入的员工的姓名:

listofemployee.stream().filter(employee->employee.getDOJ().getMonth().equals(Month.JUNE)).map(employee.getName()).forEach(System.out::println);

推荐阅读