所以我遇到的问题是程序打印出包含某个键的每个值。该程序询问用户他们想要查看员工名单的部门。然后用户选择他们想要看到的部门并输入部门旁边打印的值,让我们说他们选择了“4.会计”。然后,程序需要打印出与该键关联的所有值。格式为“会计中的员工:员工1,员工2等。它也应该使用方法countEmps。我特别需要帮助的是使程序获取具有所需键和打印值的语法他们出去了。感谢任何帮助!
import java.util.ArrayList;
import java.util.HashMap;
public class Department{
ArrayList<Employee> countEmps(HashMap <String, Employee> e , int I, ArrayList<Employee> l){
return l;
}
}
import java.util.Scanner;
import java.util.*;
public class empCountDriver {
public static void main(String[] args) {
Map <String, Employee> empMap = new HashMap<>();
int menuselect = 0;
Scanner key = new Scanner(System.in);
Employee emp1 = new Employee (101,2 ,3 , "John", "Joe");
Employee emp2 = new Employee (102,5 ,6 , "Phil", "Jognson");
Employee emp3 = new Employee (103,4 ,7 , "Billy", "Schmitz");
Employee emp4 = new Employee (104,3 ,3 , "Hank", "Dugart");
Employee emp5 = new Employee (105,1 ,2 , "Able", "Philstein");
Employee emp6 = new Employee (106,3 ,10 , "Adam", "Renolds");
Employee emp7 = new Employee (107,1 ,4 , "Larry", "Cableguy");
Employee emp8 = new Employee (108,5 ,2 , "Doug", "Dougster");
Employee emp9 = new Employee (109,2 ,3 , "Mylan", "Ester");
Employee emp10 = new Employee (110,5 ,6 , "Cherry", "Jakesn");
Employee emp11 = new Employee (111,2 ,3 , "Ethel", "Manfred");
Employee emp12 = new Employee (112,3 ,11 , "Lindsey", "Joel");
Employee emp13 = new Employee (113,1,1 , "Ellie", "Winston");
Employee emp14 = new Employee (114,4 ,6 , "Blake", "Wiotell");
Employee emp15 = new Employee (115,1 ,3 , "Elton", "John");
Employee emp16 = new Employee (116,2 ,7 , "David", "Flint");
Employee emp17 = new Employee (117,4 ,600000 , "Igor", "TheElder");
Employee emp18 = new Employee (118,5 ,9 , "Hanz", "Joe");
Employee emp19 = new Employee (119,2 ,3 , "Jordan", "Friedel");
Employee emp20 = new Employee (120,2 ,3 , "Dyaln", "Rogers");
empMap.put("2", emp1);
empMap.put("5", emp2);
empMap.put("4", emp3);
empMap.put("3", emp4);
empMap.put("1", emp5);
empMap.put("3", emp6);
empMap.put("1", emp7);
empMap.put("5", emp8);
empMap.put("2", emp9);
empMap.put("5", emp10);
empMap.put("2", emp11);
empMap.put("3", emp12);
empMap.put("1", emp13);
empMap.put("4", emp14);
empMap.put("1", emp15);
empMap.put("2", emp16);
empMap.put("4", emp17);
empMap.put("5", emp18);
empMap.put("2", emp19);
empMap.put("2", emp20);
menus();
menuselect = key.nextInt();
while (menuselect!= 6){
//if the user enters 1 it will print out all the employees with the key of 1, if the user enters 2
//it will print out all the employees with the key of 2 and so on.
}
}
public static void menus(){
System.out.println("Company Departments:");
System.out.println("1. IS");
System.out.println("2. Accounting");
System.out.println("3. Purchasing");
System.out.println("4. Sales");
System.out.println("5. Advertising");
System.out.println("6. EXIT");
}
}
员工类:
public class Employee {
private int employeeID;
private int departmentID;
private int employedYears;
private String firstName;
private String lastName;
public Employee ( int eid, int d, int y, String fn, String ln){
employeeID = eid;
departmentID = d;
employedYears = y;
firstName = fn;
lastName = ln;
}
public void setEmployeeID (int EmployeeID){
employeeID = EmployeeID;
}
public int getEmployeeID(){
return employeeID;
}
public void setDepartmentID (int DepartmentID){
departmentID = DepartmentID;
}
public int getDepartmentID(){
return departmentID;
}
public void setEmployedYears(int EmployedYears){
employedYears = EmployedYears;
}
public int getEmployedyears(){
return employedYears;
}
public void setFirstName (String FirstName){
firstName = FirstName;
}
public String getFirstName (){
return firstName;
}
public void setLastName (String LastName){
lastName = LastName;
}
public String getLastName (){
return lastName;
}
}
答案 0 :(得分:2)
您需要修复Employee
。由于你的安装人员被打破了,我删除了他们 - 你无论如何都没有使用它们,并且使一个类不可变是有好处的。您还需要实施hashCode
并覆盖equals
第二部分(我假设employeeId
(s)是唯一的)。
class Employee {
private final int employeeID;
private final int departmentID;
private final int employedYears;
private final String firstName;
private final String lastName;
public Employee(int eid, int d, int y, String fn, String ln) {
employeeID = eid;
departmentID = d;
employedYears = y;
firstName = fn;
lastName = ln;
}
@Override
public int hashCode() {
return Integer.hashCode(employeeID);
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o instanceof Employee) {
return employeeID == ((Employee) o).employeeID;
}
return false;
}
@Override
public String toString() {
return String.format("%s, %s %d %d %d", lastName, firstName, //
employeeID, departmentID, employedYears);
}
public int getEmployeeID() {
return employeeID;
}
public int getDepartmentID() {
return departmentID;
}
public int getEmployedyears() {
return employedYears;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
现在,您可以创建HashSet
Employee
(s)并按部门过滤(因为toString
已实施,您只需打印),例如
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Set<Employee> empSet = new HashSet<>(Arrays.asList(//
new Employee(101, 2, 3, "John", "Joe"), //
new Employee(102, 5, 6, "Phil", "Jognson"), //
new Employee(103, 4, 7, "Billy", "Schmitz"), //
new Employee(104, 3, 3, "Hank", "Dugart"), //
new Employee(105, 1, 2, "Able", "Philstein"), //
new Employee(106, 3, 10, "Adam", "Renolds"), //
new Employee(107, 1, 4, "Larry", "Cableguy"), //
new Employee(108, 5, 2, "Doug", "Dougster"), //
new Employee(109, 2, 3, "Mylan", "Ester"), //
new Employee(110, 5, 6, "Cherry", "Jakesn"), //
new Employee(111, 2, 3, "Ethel", "Manfred"), //
new Employee(112, 3, 11, "Lindsey", "Joel"), //
new Employee(113, 1, 1, "Ellie", "Winston"), //
new Employee(114, 4, 6, "Blake", "Wiotell"), //
new Employee(115, 1, 3, "Elton", "John"), //
new Employee(116, 2, 7, "David", "Flint"), //
new Employee(117, 4, 600000, "Igor", "TheElder"), //
new Employee(118, 5, 9, "Hanz", "Joe"), //
new Employee(119, 2, 3, "Jordan", "Friedel"), //
new Employee(120, 2, 3, "Dyaln", "Rogers")));
while (true) {
menus();
int choice = key.hasNextInt() ? key.nextInt() : 6;
if (choice == 6) {
break;
}
empSet.stream().filter(emp -> emp.getDepartmentID() == choice)
.forEach(System.out::println);
}
}
答案 1 :(得分:0)
一些代码段。请试试这个
<强>解释强>
由于在hashmap中,您不能多次添加相同的键,因此值对象将被覆盖。因此,将所有员工对象添加到数组List中,并将结果arraylist放入具有指定键的hashmap。
检索时,
Map <String, List<Employee)> empMap = new HashMap <String, ArrayList<Employee)>();
.....
Employee emp2 = new Employee (102,5 ,6 , "Phil", "Jognson");
.
.
Employee emp9 = new Employee (109,2 ,3 , "Mylan", "Ester");
List<Employee> emp2List = new ArrayList<Employee>();
emp2List.add(emp2);
emp2List.add(emp9);
empMap.put("2", emp2List );
.....
while (menuselect!= 6){
//if the user enters 1 it will print out all the employees with the key of 1, if the user enters 2
//it will print out all the employees with the key of 2 and so on.
List<Employee) empList = empMap.get(menuselect);
for (Employee employee : empList) {
.....
}
}
答案 2 :(得分:0)
这似乎与以前的许多问题重复。像这个 : HashMap with multiple values under the same key
但我可以给你一个简单的解决方案。那就是&#34;扭转你的关键,价值对&#34; 并使用如下:
HashMap<String,Integer> data = new HashMap();
data.put("Joe", 1);
data.put("Lin",1);
data.put("Haha",2);
//select apartment like below
for(Entry<String,Integer> i :data.entrySet()){
if(i.getValue()==2){
System.out.println(i.getKey());
}
}
最重要的是你的设计,这个解决方案是否符合你的整个设计?