【问题标题】:how to revoe and display objects fom Map如何从地图中删除和显示对象
【发布时间】:2015-08-13 13:32:48
【问题描述】:

我正在尝试编写一个程序,使用 Map 将员工的详细信息与员工的爱好进行映射。程序应将员工的详细信息及其爱好作为输入,然后将它们作为键/值对存储在映射中。添加详细信息完成后,用户应该能够列出所有员工以及他们的爱好,还应该能够查看特定员工的(通过提供员工 ID 作为输入)、详细信息和他/她的爱好.此外,用户应该能够根据员工的 id 删除员工。

我已经尝试如下。但是方法'deleteEmployee()'、'isEmployeePresent()'和'displayEmployees()'是错误的

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class Employee {
    String designation, name;
    Date dob;
    int employeeID;
    float salary;

    public Employee(String designation, Date dob, int employeeID, String name,
            float salary) {
        this.designation = designation;
        this.dob = dob;
        this.employeeID = employeeID;
        this.name = name;
        this.salary = salary;
    }

    public String getDesignation() {
        return designation;
    }

    public Date getDob() {
        return dob;
    }

    public int getEmployeeID() {
        return employeeID;
    }

    public String getName() {
        return name;
    }

    public float getSalary() {
        return salary;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public void setEmployeeID(int employeeID) {
        this.employeeID = employeeID;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
}

class Hobby {
    String hobbyDescription, hobbyName;

    public Hobby(String hobbyName, String hobbyDescription) {
        this.hobbyName = hobbyName;
        this.hobbyDescription = hobbyDescription;
    }

    public String getHobbyName() {
        return hobbyName;
    }

    public String getHobbyDescription() {
        return hobbyDescription;
    }

    public void setHobbyName() {
        this.hobbyName = hobbyName;
    }

    public void setHobbyDescription() {
        this.hobbyDescription = hobbyDescription;
    }
}

public class EmployeeManagement {
    public static void main(String args[]) {
        HashMap<Employee, Hobby> hm1 = new HashMap<Employee, Hobby>();
        Scanner sc1 = new Scanner(System.in);
        System.out.println("How many names do you want to add to the system? ");
        int w = sc1.nextInt();
        EmployeeManagement ob = new EmployeeManagement();
        ob.addEmployees(w, hm1);
        ob.displayEmployees(hm1);
        while (true) {
            System.out.println("1. Delete an employee");
            System.out
                    .println("2. Check whether an employee is present or not");
            System.out.println("3. Break the loop and terminates the program");
            int ch = sc1.nextInt();
            switch (ch) {
            case 1:
                System.out.println("Enter Employee-I'd:");
                int p = sc1.nextInt();
                ob.deleteEmployee(p, hm1);
                ob.displayEmployees(hm1);
                break;
            case 2:
                System.out.println("Enter Employee-I'd:");
                int q = sc1.nextInt();
                boolean b = ob.isEmployeePresent(q, hm1);
                if (b == true)
                    System.out.println("The employee is present");
                else
                    System.out.println("The employee is not present");
                break;
            case 3:
                return;
            }
        }
    }

    public void addEmployees(int n, Map<Employee, Hobby> abc) {
        Scanner sc = new Scanner(System.in);
        Employee[] obj1 = new Employee[n];
        Hobby[] obj2 = new Hobby[n];
        for (int i = 0; i < n; i++) {
            System.out.println("Enter Employee name:");
            String nm = sc.next();
            System.out.println("Enter designation:");
            String des = sc.next();
            System.out.println("Enter employeeI-D:");
            int eId = sc.nextInt();
            System.out.println("Enter salary:");
            double sl1 = sc.nextDouble();
            float sl = (float) sl1;
            System.out.println("Enter Hobby-name:");
            String hnm = sc.next();
            System.out.println("Enter Hobby-description");
            String hdes = sc.next();
            System.out.println("Enter date-of-birth:");
            String dt1 = sc.next();
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            try {
                Date dt = formatter.parse(dt1);
                obj1[i] = new Employee(des, dt, eId, nm, sl);
                obj2[i] = new Hobby(hnm, hdes);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            abc.put(obj1[i], obj2[i]);
        }
    }

    public void deleteEmployee(int id, Map<Employee, Hobby> m1) {
        if (m1.containsKey((Employee.getEmployeeID()) == id))
            m1.remove(Employee);
    }

    public boolean isEmployeePresent(int id, Map<Employee, Hobby> m2) {
        if (m2.containsKey((Employee.getEmployeeID()) == id))
            return true;
        else
            return false;
    }

    public void displayEmployees(Map<Employee, Hobby> hm) {
        for (Map.Entry<Employee, Hobby> m : hm.entrySet()) {
            System.out.println(m.getKey() + " " + m.getValue());
        }
    }
}

【问题讨论】:

  • 错误?在什么意义上?
  • containsKey(key) etc. 需要您用于条目的密钥,即您需要Employee 类型的密钥。

标签: java dictionary


【解决方案1】:

您正在尝试以静态方式访问 Employee 方法:

if(m1.containsKey((Employee.getEmployeeID())==id))
//                 ↑ here

您必须在地图中获取Employee 的实例,然后与给定的id 进行比较:

public void deleteEmployee(int id,Map<Employee,Hobby> m1) {
    // iterate in the map searching for the id
    for (Entry<Employee, Hobby> entry : map.entrySet()) {
        Employee e = entry.getKey();   // here you have the employee
        Hobby h = entry.getValue();    // here you have the hobby

        // check if this employee have same id than given
        if(e.getEmployeeID() == id) { // map contains the employee
            m1.remove(e);
    }
}

注意事项:

  • 我建议返回一个布尔值来检查员工是否已被更新、删除等...public boolean deleteEmployee(int id,Map&lt;Employee,Hobby&gt; m1)

  • 我会以另一种方式保留员工,HashMap 不允许重复,因此如果您不将 HashMap&lt;Employee, List&lt;Hobby&gt;&gt; 声明为每个员工,则您不能在每个员工中保存多个 Hobby

【讨论】:

  • displayEmployees(Map hm) 怎么样??根据我给出的代码 sn-p,它会打印一些任意的垃圾值。我输入了两个员工信息,这个函数显示如下 Employee@6bc7c054 Hobby@232204a1 Employee@75b84c92 Hobby@4aa298b7
  • 垃圾值??它打印默认 toString... 只需覆盖 toString 或引用类的属性,如 e.getName()
  • ok 很好... 解决了这个问题... 但现在问题出现在 void deleteEmployee(int id, Map m1) 函数... 现在函数如下: public void deleteEmployee(int id,Map m1) { for (Map.Entry entry : m1.entrySet()) { Employee e = entry.getKey();爱好 h = entry.getValue(); if(e.getEmployeeID() == id) { m1.remove(e); } } }
  • 程序编译成功...映射中也添加了条目...但是当我调用此函数时,它会引发以下异常--线程“main”中的异常 java.util.ConcurrentModificationException at java .util.HashMap$HashIterator.nextNode(Unknown Source) at java.util.HashMap$EntryIterator.next(Unknown Source) at java.util.HashMap$EntryIterator.next(Unknown Source) at EmployeeManagement.deleteEmployee(EmployeeManagement.java:170 ) 在 EmployeeManagement.main(EmployeeManagement.java:113)
猜你喜欢
  • 2020-10-26
  • 2015-07-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 2021-02-25
相关资源
最近更新 更多