【问题标题】:How can I solve with Java's map containsKey() method?如何使用 Java 的 map containsKey() 方法解决?
【发布时间】:2021-12-25 00:26:57
【问题描述】:

在键入 ADD 时,我检查了代码并将数据保存到 HashMap 是正确的。然后在选择 FIND 选项后,我可以使用专用功能,但即使 100% 正确,该方法也无法显示找到的对象。 请检查此代码并告诉我为什么它在“public void showInfo(String name, String secondName)”中找不到正确的对象,该对象由在 CompanyApp 类中键入“FIND”引发的 Company 类


import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;

public class CompanyApp {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        options[] values = options.values();
        int choose;
        int EXIT_NR = 2;
        Company ref = new Company();
        do {
            System.out.println("Available options: ");
            for (options one : values) {
                System.out.println(one.getDescription() + " - " + one.name());
            }
            System.out.println("Choose one: ");
            try {
                choose = options.valueOf(in.nextLine()).ordinal();
                if (Objects.equals(EXIT_NR, choose)) break;
                if (choose < 0 || choose >= options.values().length) {
                    throw new IllegalArgumentException("Choose 0, 1 or 2!");
                }
                options(choose);
            } catch (InputMismatchException e) {
                System.out.println("Choose a number ");
            }


        } while (1 == 1);
    }

    static void options(int choose){
        Company ref = new Company();
        Scanner info = new Scanner(System.in);

        switch (choose){
            case 0:
                System.out.println("Type in the name of the worker: ");
                String name = info.nextLine();
                System.out.println("Type in the second name of the worker: ");
                String secondName = info.nextLine();
                System.out.println("Type in the salary: ");
                double salary = info.nextDouble();
                info.nextLine();
                ref.add(new Employee(name, secondName, salary));
                break;
            case 1:
                System.out.println("Type in the name of the worker you want to find: ");
                String name2 = info.nextLine();
                System.out.println("Type in the second name of the worker you want to 
                find: ");
                String secondName2 = info.nextLine();
                ref.showInfo(name2, secondName2);
                break;
        }
    }
}

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Company {
    private Map<String, Employee> map = new HashMap<>();

    public void add(Employee employee){
        String key = employee.getName() + " " + employee.getSecondName();
        if(!map.containsKey(key)){
        map.put(key, employee);
            System.out.println("Added object to map");}

    }

    public void showInfo(String name, String secondName){

        String key = name + " " + secondName;
        System.out.println("in showinfo method");
        if(map.containsKey(key)){
            System.out.println("found an object");
            Employee employee = map.get(key);
            System.out.println(employee.getName());
        }}}  

enum options {

     ADD("Add employee "), FIND("Find employee"), EXIT("Exit program");
    private String description;

     options(String description) {
         this.description = description;
     }

     public String getDescription() {
         return description;
     }

     public void setDescription(String description) {
         this.description = description;
     }

     @Override
     public String toString() {
         return "options{" +
                 "description='" + description + '\'' +
                 '}';
     }
 }

    String name;
    String secondName;
    double salary;

    public Employee(String name, String secondName, double salary) {
        this.name = name;
        this.secondName = secondName;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

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

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public double getSalary() {
        return salary;
    }

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





    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", secondName='" + secondName + '\'' +
                ", salary=" + salary +
                '}';
    }
}


【问题讨论】:

    标签: java dictionary containskey


    【解决方案1】:

    问题在于方法static void options(int choose)。你需要传递Company-Object 并像这样在那里使用它:

    从主方法调用(ref 是您在主方法中创建的Company-Object)

    options(choose, ref);
    

    options 方法以 Company 作为第二个参数:

      static void options(int choose, Company ref){
        Scanner info = new Scanner(System.in);
    
        switch (choose){
          case 0:
            System.out.println("Type in the name of the worker: ");
            String name = info.nextLine();
            System.out.println("Type in the second name of the worker: ");
            String secondName = info.nextLine();
            System.out.println("Type in the salary: ");
            double salary = info.nextDouble();
            info.nextLine();
    
            //use the passed Company here
            ref.add(new Employee(name, secondName, salary));
            break;
          case 1:
            System.out.println("Type in the name of the worker you want to find: ");
            String name2 = info.nextLine();
            System.out.println("Type in the second name of the worker you want to find: ");
            String secondName2 = info.nextLine();
     
            //and here
            ref.showInfo(name2, secondName2);
            break;
        }
      }
    

    解释你的代码发生了什么

    如前所述,问题出在方法static void options(int choose)

    在这里您创建一个新的Company-Object,它不会以任何方式传递给 main 方法。

    当您使用 ADD 和之后的 FIND 时会发生这种情况:

    1. ADD从主方法调用options
    2. 新的Company-对象在options中创建
    3. 新的Employee-Object 从上一点添加到Company
    4. 方法结束 -> 创建的Company-Object 被“丢弃”(符合垃圾收集条件)
    5. FIND从主方法调用options
    6. 新的Company-对象在options中创建(因此其中没有员工)
    7. 找不到Employee,因为新创建的Company的map中没有条目

    【讨论】:

      【解决方案2】:

      当您尝试使用 FIND 选项从中获取数据时,地图为空。原因是您在 options 方法中重新创建 Company 对象:
      公司参考 = 新公司();
      同时,地图也被重新创建,所以里面没有记录。

      另外,main方法中的Company对象也没有使用。

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多