gpt4 book ai didi

java - 将对象数组放入 Map 和 Set 中

转载 作者:行者123 更新时间:2023-11-30 02:45:41 25 4
gpt4 key购买 nike

在 EmployeeSearch 的 main 方法中,创建一个由多个 Employee 对象组成的数组,其中一个包含您的名字和姓氏,并使用您的电子邮件 ID 作为 ID。使用 addEmployees 方法将每个 Employee 放入 Map 和 Set 中。使用增强的 for 循环。 (您可以使用 getID 方法检索员工的 ID 以用作 Map 的键。)

搜索 map 。提示用户搜索 map 所需的任何信息。使用 get 方法进行搜索。

我尝试了几种方法,不知道哪种方法是正确的;我评论了我认为可能的解决方案,但我不确定还能做什么。

我是否正确创建了对象数组?像这样 - ArrayList 员工 = new ArrayList();或 Employee 员工 = new Employee("John", "Bowman", "970");?然后我需要搜索,这就是 addEmployees 方法应该做的事情,但我不断收到错误。

public class EmployeeSearch {

public static void main(String[] args) {

public Set<Employee> empSet = new HashSet<Employee>();

public Map<String, Employee> empMap = new HashMap<String, Employee>();
// empMap.put("John", "970");

ArrayList<String> employees = new ArrayList<String>();
//empMap.put("John", employees);
employees.add("John");
employees.add("Bowman");
employees.add("970");
System.out.println("Employee:");

for (String employeeName : employees){
System.out.println(employeeName);
}

//Employee employees = new Employee("John", "Bowman", "970");
//empSet.add("John", "Bowman", "970");
}

public void addEmployees(Employee[] employees) {
// empSet.add(employees[0]);
// empMap.put(employees[0].getID(),employees[0]);
// for(empSet.add(employees[e])):empMap.put(employees[e]){

}

}

----------------------更新------------------------ --------------------

public class EmployeeSearch {
public static void main(String[] args) {
Set<Employee> empSet = new HashSet<Employee>();

public Map<String, Employee> empMap = new HashMap<String, Employee>();

public Employee emp1 = new Employee("John", "Bowman", "970");

ArrayList<Employee> employees = new ArrayList<Employee>();

employees.add(emp1);
empMap.put("970", emp1);


for (Employee employeeName : employees) {
System.out.println(employeeName);
}
}




public void addEmployees(Employee[] employees){
empSet.add(addEmployees(employees));
empMap.put(employees.getID(), addEmployees(employees));
Employee searchedFor = empMap.get(id);
if (searchedFor == null) {
System.out.println();
}else{
System.out.println();
}
}

}

我不断收到“无法解析符号”错误,并且显示“使用 addEmployees 方法将每个 Employee 放入 Map 和 Set 中”。我很确定我在 main 中执行了此操作,但不知道如何将其放入 addEmployees 方法中。

最佳答案

如果您尝试将员工添加到数组列表中,您所要做的就是:

//Create a single employee
Employee employee = new Employee("John", "Doe", "Address");

//Create array list
ArrayList<Employee> employees = new ArrayList<Employee>();

//Add employee to list
employees.add(employee);

//repeat as needed for each employee

如果你想要一个 HashMap ,你可以这样做:

//Create a single employee
Employee employee = new Employee("John", "Doe", "Address");

//Create array list
HashMap<String, Employee> employees = new ArrayList<String, Employee>();

//Add employee to list
employees.put("email@example.com", employee);

//repeat as needed for each employee

如果是我,我会使用 HashMap ,因为您可以非常轻松地搜索它,在本例中使用电子邮件,执行以下操作:

    //search for employee
if( employees.containsKey("email@example.com")){
//retrieve employee object
Employee myGuy = employees.get("email@example.com");

}

关于java - 将对象数组放入 Map 和 Set 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272322/

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