[英]Javassist CannotCompileException when trying to add values to a Map<String,Object>
[英]Map<String, Object> - Search Object and add certain values to a set
繼上一個問題之后,我想遍歷 map 中的 Object 值,如果找到某些結果,請將它們添加到 Set/List,然后返回該 Set/List。
我的雇主代碼 Class:
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);
}
第二 Object 代碼:
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)
我需要的方法是查找在“服務台”中工作的所有員工,例如,將它們添加到集合並返回該集合。 如果沒有找到該 position 的消息,則返回一條一般消息,指出沒有找到可以在那里工作的消息。
我正在努力掌握如何做到這一點。
@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");
}
打印(使用下面的toString
和fullname
建議)
JohnSmith3, 22, Paris, Service Desk
JohnSmith2, 23, Paris, Service Desk
這是方法。 它
public Set<NewHire> findEmployees(String jobTitle) {
return employee.values()
.stream()
.filter(emp->emp.jobTitle.equalsIgnoreCase(jobTitle))
.collect(Collectors.toSet());
}
幾個建議。
findEmployees
public String toString() {
return name + ", " + age + ", " + location + ", " + jobTitle;
}
使用 Stream 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 class 中實現了 getJobTitle 和 toString 方法...
下面給出了您應該如何在Employer
class 中實施您的方法:
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;
}
Output:
System.out.println(CompanyA.findEmployees("Service Desk"));
如下:
[NewHire [age=22, location=Paris, jobTitle=Service Desk], NewHire [age=23, location=Paris, jobTitle=Service Desk]]
假設:
NewHire
中實現了public
getter 和 setter。NewHire
中實現了toString()
方法,如下所示(這是 eclipse 自動生成的代碼):@Override
public String toString() {
return "NewHire [age=" + age + ", location=" + location + ", jobTitle=" + jobTitle + "]";
}
補充說明:
您應該遵循Java 命名約定,例如變量CompanyA
應命名為companyA
。
方法的名稱應該是自描述的,例如方法名稱, 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.
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.