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

Java 如何从员工列表打印在特定月份加入的员工列表?,java,hashmap,Java,Hashmap,如何从员工列表打印在特定月份加入的员工列表 您好,我正在打印在“六月”加入的员工名单? 下面是我的代码, Pojo类:- import java.time.LocalDate; public class Employee { private String name; private String empID; private Designation designation; private LocalDate dateOfJoining; privat

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

您好,我正在打印在“六月”加入的员工名单? 下面是我的代码, 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为键,name为值。但我无法继续。有人能帮忙吗? 下面是我的代码:-

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);
import java.time.LocalDate;
导入java.time.Period;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公营雇员可比{
公共静态void main(字符串[]args){
员工emp=新员工();
JoiningDate jd=新的JoiningDate();
List-listofemployee=new-ArrayList();
新增员工名单(新员工(“粉红”、“12345”、Designation.ASE、jd.date1、20000”);
添加(新员工(“红色”,“24680”,Designation.SE,jd.date2,30000));
新增员工名单(新员工(“蓝色”、“13570”、Designation.SSE、jd.date3、40000”);
添加(新员工(“橙色”、“13690”、Designation.TL、jd.date4、60000”);
新增员工名单(新员工(“绿色”、“10909”、名称、jd.Date 5、800000));
新增员工名单(新员工(“黄色”、“89076”、名称.M、jd.Date62000”);
LocalDate today=LocalDate.now();
System.out.println(今天);
时段时间1=时段之间(jd.date1,今天);
时段时间2=时段之间(jd.date2,今天);
时段时间3=时段之间(jd.date3,今天);
时段时间4=时段之间(jd.date4,今天);
时段时间5=时段之间(jd.date5,今天);
时段时间6=时段之间(jd.date6,今天);
Map hashmap=新hashmap();
适用于(员工:员工名单){
LocalDate key=employee.getDOJ();
字符串值=employee.getName();
if(hashmap.containsKey(键)){
ArrayList=新建ArrayList();
列表。添加(员工);
hashmap.put(键、值);
}
}
System.out.println(hashmap);

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

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

但是这在重复的情况下不起作用

为什么你不检查它们的jining date=6?如果你需要返回它而不是
。forEach(…);
使用
.collector(Collectors.toList());
Hi,谢谢!但实际上它将使用(key.getMonthValue()==05)而不是if(key.getMonth()==6)。
    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.
          }

     }
    for (Employee employee : listofemployee) {
        LocalDate key = employee.getDOJ();
        String value = employee.getName();
        if (key.getMonthValue() == 06) {
            hashmap.put(key, value);
        }
    }
    System.out.println(hashmap);