gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:40:05 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于java - 如何从员工列表中打印特定月份加入的员工列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com