簡體   English   中英

如何使用 Java 的 map containsKey() 方法解決?

[英]How can I solve with Java's map containsKey() method?

我檢查了代碼並在鍵入 ADD 時將數據保存到 HashMap 是正確的。 然后在選擇選項 FIND 后,我可以進入專用功能,但該方法無法向我顯示找到的對象,即使它 100% 正確。 請檢查此代碼並告訴我為什么它沒有在“public void showInfo(String name, String secondName)”中找到由類 CompanyApp 中的 TYPING“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 +
                '}';
    }
}


問題出在方法static void options(int choose) 您需要傳遞Company -Object 並在那里使用它,如下所示:

從 main 方法調用( ref是您在 main 方法中創建的Company -Object)

options(choose, ref);

options -method 以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從 main 方法調用options
  6. Company對象是在options創建的(因此其中沒有員工)
  7. 找不到Employee ,因為在新創建的Company的地圖中沒有條目

當您嘗試使用 FIND 選項從中獲取數據時,該地圖是空的。 原因是您在 options 方法中重新創建了 Company 對象:
公司 ref = new Company();
同時,地圖也被重新創建,因此里面沒有記錄。

此外,未使用 main 方法中的 Company 對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM