映射<字符串,对象> - 搜索对象并将某些值添加到集合中



继上一个问题之后,我想迭代映射中的Object值,如果找到某些结果,请将它们添加到Set/List,然后返回该Set/List。

我的雇主类代码:

public class Employer {
Map<String, NewHire> employee = new HashMap<>();
}
public void addEmployee(String fullName, String age, String location, String JobTitle) {
NewHire newEmployee = new NewHire(age, location, JobTitle);
this.employee.put(fullName, newEmployee);
}

第二个目标代码:

public class NewHire {
private String age;
private String location;
private String jobTitle;
}
public NewHire(String aAge, String aLocation, String aJobTitle) {
this.age = aAge;
this.location = aLocation;
this.jobTitle = aJobTitle;
}

我添加了以下内容:

Employer CompanyA = new Employer();
CompanyA.addEmployee("JohnSmith1", "22", "London", "Front Office");
CompanyA.addEmployee("JohnSmith2", "23", "Paris", "Service Desk");
CompanyA.addEmployee("JohnSmith3", "22", "Paris", "Service Desk");
CompanyA.addEmployee("JohnSmith4", "21", "New York", "HR");

我现在想添加以下方法:

public NewHire findAnEmployee(String jobTitle)

我需要的方法是查找在"服务台"工作的所有员工,例如,将他们添加到集合中并返回该集合。如果找不到该职位的人,则返回一条一般消息,说明找不到在那里工作的人。

我正在努力掌握如何做到这一点。

下面给出的是如何在Employer类中实现方法:

public Set<NewHire> findEmployees(String jobTitle) {
Set<NewHire> result = new HashSet<NewHire>();
for (Entry<String, NewHire> entry : employee.entrySet()) {
NewHire newHire = entry.getValue();
if (newHire.getJobTitle().equals(jobTitle)) {
result.add(newHire);
}
}
if (result.size() == 0) {
System.out.println("Nobody is found for that position");
}
return result;
}

输出:

System.out.println(CompanyA.findEmployees("Service Desk"));的输出如下:

[NewHire [age=22, location=Paris, jobTitle=Service Desk], NewHire [age=23, location=Paris, jobTitle=Service Desk]]

假设:

  1. 您已经在类NewHire中实现了publicgetter和setter
  2. 您已经在类NewHire中实现了toString()方法,如下所示(这是eclipse自动生成的代码(:
@Override
public String toString() {
return "NewHire [age=" + age + ", location=" + location + ", jobTitle=" + jobTitle + "]";
}

附加说明:

  1. 您应该遵循Java命名约定,例如变量CompanyA应该命名为companyA

  2. 方法的名称应该是自描述性的,例如方法名称findAnEmployee应该改为findEmployees,正如您所提到的,What I need the method to do is looks for all Employees that work in "Service Desk" for example, add them to a Set and return that Set.

使用流API:

public Set<NewHire> findAnEmployee(String jobTitle) {
return employee.values().stream().filter(e -> e.getJobTitle().equals(jobTitle)).collect(Collectors.toSet());
}
CompanyA.findAnEmployee("Service Desk").stream().forEach(System.out::println);

我在NewHire类中实现了getJobTitle和toStringMethot。。。

@JohannesKuhn的建议很好。以下是如何实现它。

Set<NewHire> newEmp = findEmployees("Service Desk");
if (!newEmp.isEmpty()) {
newEmp.forEach(System.out::println);
} else {
System.out.println("No records found for job title");
}

打印(使用下面的toStringfullname建议(

JohnSmith3, 22, Paris, Service Desk
JohnSmith2, 23, Paris, Service Desk

这是方法。它

  • 创建一个映射值流(它们是NewHire实例(
  • 对职务进行筛选
  • 并将它们收集成一组

public Set<NewHire> findEmployees(String jobTitle) {
return employee.values()
.stream()
.filter(emp->emp.jobTitle.equalsIgnoreCase(jobTitle))
.collect(Collectors.toSet());      
}

几个建议。

  • 将全名添加到NewHire类中
  • 将方法名称更改为findEmployees
  • 重写NewHire的toString方法以返回相关信息的字符串。例如
public String toString() {
return name + ", " + age + ", " + location + ", " + jobTitle;
}

最新更新