【问题标题】:Map<String, Object> - Search Object and add certain values to a setMap<String, Object> - 搜索对象并将某些值添加到集合中
【发布时间】:2020-04-18 15:43:26
【问题描述】:

继上一个问题之后,我想遍历我的地图中的对象值,如果找到某些结果,将它们添加到集合/列表中,然后返回该集合/列表。

我的雇主类别代码:

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)

我需要该方法做的是查找在“服务台”中工作的所有员工,例如,将它们添加到集合并返回该集合。如果找不到该职位的人,则返回一条一般性消息,指出没有人在该职位上工作。

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

【问题讨论】:

  • 为什么不返回一个空集?
  • NewtoJavaHelp - 如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。接受的答案有助于未来的访问者自信地使用该解决方案。查看meta.stackexchange.com/questions/5234/… 了解如何操作。

标签: java dictionary collections set


【解决方案1】:

使用流 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 和 toString 方法...

【讨论】:

    【解决方案2】:

    @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;
            }
    

    【讨论】:

    • 其他建议...如果该方法可以返回一个集合..最好将其重命名为findEmployees
    • 好建议,我加了。
    【解决方案3】:

    下面是你应该如何在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 类中实现了 public getter 和 setter。
    2. 你已经在NewHire类中实现了toString()方法如下(这是eclipse自动生成的代码):
    @Override
    public String toString() {
        return "NewHire [age=" + age + ", location=" + location + ", jobTitle=" + jobTitle + "]";
    }
    

    补充说明:

    1. 您应该关注Java naming conventions,例如变量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.

    【讨论】:

    • Entry 是一个类型化的对象。如果您将for loop 中的类型正确分配为Entry&lt;String,NewHire&gt;,则不必强制转换entry.getValue()
    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2013-12-18
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多