如何使用Java的map包含Key()方法解决?



在键入ADD时,我检查了代码,并将数据保存到HashMap中是正确的。然后在选择选项FIND后,我可以进入专用函数,但该方法无法显示我找到的对象,即使它是100%正确的。请检查这个代码并告诉我为什么它在"中找不到正确的对象;public void showInfo(字符串名称,字符串secondName("对于由TYPING引发的类公司";FIND";同类CompanyApp


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-对象并在那里使用它,如下所示:

从主方法调用(refCompany-在主方法中创建的对象(

options(choose, ref);

Company为第二参数的options-方法:

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-对象,它不会以任何方式传递给主方法。

这就是当你使用ADD和FIND之后会发生的情况:

  1. 使用ADD从主方法调用options
  2. 新的Company-在options中创建对象
  3. 新的Employee-对象从上一点添加到Company
  4. 该方法结束->所创建的CCD_;扔掉";(符合垃圾收集条件(
  5. FIND从主方法调用options
  6. 新的Company-对象在options中创建(因此其中没有Employees(
  7. 找不到Employee,因为在新创建的Company的映射中没有条目

当您尝试使用FIND选项从地图中获取数据时,地图是空的。原因是您在options方法中重新创建了Company对象:
Company ref=new Company((
同时还重新创建了地图,因此里面没有记录。

此外,不使用主方法中的"公司"对象。

最新更新