如何要求用户输入哈希表的键和 return 对象值
How to ask user to enter a key for a hashtable and return the object value
我有一个哈希表 (theTable),其中字符串用户名作为键,对象员工作为值。我想要求用户输入用户名,如果用户输入的用户名等于表中的键,我想 return 员工对象值。
System.out.println("Enter your username: ");
Scanner sc = new Scanner(System.in);
String UserSearch = sc.nextLine();
if UserSearch == theTable.get(key)(){
return Employee;
}
theTable.get 将 return 映射键,但我不确定如何 check/get 键等于输入的用户名。
我建议为此使用 HashMap
Map<String, Object> theTable = new HashMap<>();
theTable.put("employeeId12345", new Employee());
System.out.println("Enter your username: ");
Scanner sc = new Scanner(System.in);
String userSearch = sc.nextLine(); // assume it will return employeeId12345
if (theTable.contains(userSearch )){
return theTable.get(userSearch);
}
我有一个哈希表 (theTable),其中字符串用户名作为键,对象员工作为值。我想要求用户输入用户名,如果用户输入的用户名等于表中的键,我想 return 员工对象值。
System.out.println("Enter your username: ");
Scanner sc = new Scanner(System.in);
String UserSearch = sc.nextLine();
if UserSearch == theTable.get(key)(){
return Employee;
}
theTable.get 将 return 映射键,但我不确定如何 check/get 键等于输入的用户名。
我建议为此使用 HashMap
Map<String, Object> theTable = new HashMap<>();
theTable.put("employeeId12345", new Employee());
System.out.println("Enter your username: ");
Scanner sc = new Scanner(System.in);
String userSearch = sc.nextLine(); // assume it will return employeeId12345
if (theTable.contains(userSearch )){
return theTable.get(userSearch);
}