猿问

如何从员工名单中打印特定月份入职的员工名单?

如何从员工名单中打印特定月份入职的员工名单?


您好,我正在尝试打印假设“六月”加入的员工名单?下面是我的代码,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;

    }


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


慕姐8265434
浏览 128回答 3
3回答

元芳怎么了

如果您只想打印 6 月份加入的员工姓名:listofemployee.stream().filter(employee->employee.getDOJ().getMonth().equals(Month.JUNE)).map(employee.getName()).forEach(System.out::println);

慕尼黑5688855

   for (Employee employee : listofemployee) {        LocalDate key = employee.getDOJ();        String value = employee.getName();        if (key.getMonthValue() == 06) {            hashmap.put(key, value);        }    }    System.out.println(hashmap);但这在重复的情况下不起作用

HUWWW

    for(Employee employee: listofemployee)     {        LocalDate key = employee.getDOJ();        String value = employee.getName();        if(key.getMonth() == 6 )         {            hashMap.put(key, value); //if the date of joining is same day and time it would replace, as map dont share dups.          }     }
随时随地看视频慕课网APP

相关分类

Java
我要回答