Java Apache公共多值映射迭代

Java Apache公共多值映射迭代,java,apache-commons,multimap,multivalue,Java,Apache Commons,Multimap,Multivalue,我有一个多重映射假设公司,它以字符串作为键,另一个多重映射假设员工作为值。Employee Multimap将字符串作为键,另一个Multimap作为值。我的问题是如何检索和迭代存储在multimap中的multimap?我正在使用apachecommonmultimap 示例:compMap有2家公司。CompA和CompB 每个公司有10名员工。(员工可以出现多次,因此使用multimap) Coll包含compA的employee multimap,但是如何从employee multim

我有一个多重映射假设公司,它以字符串作为键,另一个多重映射假设员工作为值。Employee Multimap将字符串作为键,另一个Multimap作为值。我的问题是如何检索和迭代存储在multimap中的multimap?我正在使用apachecommonmultimap

示例:compMap有2家公司。CompA和CompB 每个公司有10名员工。(员工可以出现多次,因此使用multimap)

Coll包含compA的employee multimap,但是如何从employee multimap检索特定员工(如果他出现4次)

代码:

我希望下面的场景是您所期待的。
单位
---系
---班级
---雇员
-身份证
-名字
包com.java.examples;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.Map.Entry;
班级员工{
}
阶级公司{
}
班级部{
}
公共类Test1{
公共静态void main(字符串[]args){
Map inputEmployeeMap=新HashMap();
inputEmployeeMap.put(“1”,newEmployee());
inputEmployeeMap.put(“2”,newEmployee());
inputEmployeeMap.put(“3”,newEmployee());
Map inputClassList=新建HashMap();
inputClassList.put(“class1”,inputEmployeeMap);
inputClassList.put(“class2”,inputEmployeeMap);
Map inputDeptList=new HashMap();
inputDeptList.put(“ece”,inputClassList);
inputDeptList.put(“csc”,inputClassList);
Map inputCompanyList=新的HashMap();
inputCompanyList.put(“ABCCompany”,inputDeptList);
//解析图
解析多值映射(inputCompanyList);
}
私有静态多值映射(
映射输入公司列表){
迭代器it=inputCompanyList
.entrySet().iterator();
while(it.hasNext()){
Map.Entry companyList=(Map.Entry)it.next();
System.out.println(“公司名称=“+companyList.getKey());//公司
//名字
Map deptList=(Map)companyList.getValue();//部门列表
//部门列表的迭代器。
迭代器companyListIterator=deptList.entrySet().Iterator();
while(companyListIterator.hasNext()){
Map.Entry部门列表=(Map.Entry)公司注册人
.next();
System.out.print(“Dept Name=“+departmentList.getKey());//department
//类图
Map classList=(Map)departmentList.getValue();
迭代器departmentListIterator=classList.entrySet()
.iterator();
Map.Entry empNames=(Map.Entry)departmentListIterator.next();
//类名
System.out.print(“类名=“+empNames.getKey());
//员工地图
Map empNamesMap=(Map)empNames.getValue();
迭代器eNamesIt=empNamesMap.entrySet().Iterator();
while(eNamesIt.hasNext()){
Map.Entry name=(Map.Entry)eNamesIt.next();
//打印emp Id
System.out.print(“id”+name.getKey());
System.out.print(“name=“+name.getValue());
}
System.out.println(“\n”);
}
System.out.println(“\n”);
}
}
}
希望能有帮助。

您面临的问题是因为一名员工不应在一个集合中出现两次。如果将两个值存储在多值映射的同一个键下,则这些值是不同的值(尽管它们可以通过同一个键访问)

您应该将数据模型迁移到更面向对象的模型中,而不要使用位于另一个密钥存储容器(map-insidemap)中的密钥存储容器

考虑以下仅使用集合/列表的模型。值根据equals/hashcode进行区分。若你们使用列表,你们可以在公司里有很多员工(若你们真的需要,他们可以有相同的名字)。为什么使用多值映射将其复杂化

public class AbcTest {
  public static void main(String[] args) {

    Address address1 = new Address("street1");
    Address address2 = new Address("street2");

    Employee employee1 = new Employee("employee1");
    employee1.getAddresses().add(address1);
    employee1.getAddresses().add(address2);

    Employee employee2 = new Employee("employee2");
    employee2.getAddresses().add(address2);

    Company companyA = new Company("compA");

    companyA.getEmployees().add(employee1);
    companyA.getEmployees().add(employee1); // you can add employee to the list as many times as you want, if you really need this?
    companyA.getEmployees().add(employee2);

    // now to get the employee with give name simly iterate over list of employees

    Iterator<Employee> employeeIt = companyA.getEmployees().iterator();


    Employee wantedEmployee = null;

    while (employeeIt.hasNext() && (wantedEmployee == null)) {
      Employee next = employeeIt.next();

      if (next.getName().equals("employee1")) {
        wantedEmployee = next;
      }
    }

    System.out.println(wantedEmployee);


  }



}


class Company {
  final String name;

  final List<Employee> employees;

  Company(String name) {
    this.name = name;
    this.employees = new ArrayList<>();
  }

  public String getName() {
    return name;
  }

  public List<Employee> getEmployees() {
    return employees;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if ((o == null) || (getClass() != o.getClass())) {
      return false;
    }

    Company company = (Company) o;

    if ((employees != null) ? (!employees.equals(company.employees)) : (company.employees != null)) {
      return false;
    }
    if ((name != null) ? (!name.equals(company.name)) : (company.name != null)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = (name != null) ? name.hashCode() : 0;
    result = (31 * result) + ((employees != null) ? employees.hashCode() : 0);
    return result;
  }
}

class Employee {
  final String name;
  final Set<Address> addresses;

  Employee(String name) {
    this.name = name;
    this.addresses = new HashSet<>();
  }

  public String getName() {
    return name;
  }

  public Set<Address> getAddresses() {
    return addresses;
  }

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

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if ((o == null) || (getClass() != o.getClass())) {
      return false;
    }

    Employee employee = (Employee) o;

    if ((addresses != null) ? (!addresses.equals(employee.addresses)) : (employee.addresses != null)) {
      return false;
    }
    if ((name != null) ? (!name.equals(employee.name)) : (employee.name != null)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = (name != null) ? name.hashCode() : 0;
    result = (31 * result) + ((addresses != null) ? addresses.hashCode() : 0);
    return result;
  }
}


class Address {
  final String street;

  Address(String street) {
    this.street = street;
  }

  public String getStreet() {
    return street;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if ((o == null) || (getClass() != o.getClass())) {
      return false;
    }

    Address address = (Address) o;

    if ((street != null) ? (!street.equals(address.street)) : (address.street != null)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    return (street != null) ? street.hashCode() : 0;
  }
}
公共类AbcTest{
公共静态void main(字符串[]args){
地址1=新地址(“街道1”);
地址2=新地址(“街道2”);
雇员雇员1=新雇员(“雇员1”);
employee1.getAddresses().add(address1);
employee1.getAddresses().add(address2);
雇员雇员2=新雇员(“雇员2”);
employee2.getAddresses().add(address2);
公司A=新公司(“compA”);
companyA.getEmployees().add(employee1);
companyA.getEmployees().add(employee1);//如果确实需要,您可以将员工添加到列表中任意次数?
companyA.getEmployees().add(employee2);
//现在,要获取名为的员工,请简单地迭代员工列表
迭代器employeeIt=companyA.getEmployees().Iterator();
员工wantedEmployee=null;
while(employeeIt.hasNext()&&(wantedEmployee==null)){
Employee next=employeeIt.next();
if(next.getName().equals(“employee1”)){
wantedEmployee=next;
}
}
System.out.println(wantedEmployee);
}
}
阶级公司{
最后的字符串名;
最终员工名单;
公司(字符串名称){
this.name=名称;
this.employees=new ArrayList();
}
公共字符串getName(){
返回名称;
}
公开名单{
返回员工;
}
@凌驾
公共布尔等于(对象o){
if(this==o){
返回真值
I hope the below you are expecting the below scenario.

    Company<Map>
      --- Dept<Map>
            ---Classes<Map>
                  --- Employee<Map>
                             -id
                              -name

    package com.java.examples;

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;

    class Employee {

    }

    class Company {

    }

    class Dept {

    }

    public class Test1 {

        public static void main(String[] args) {

            Map<String, Employee> inputEmployeeMap = new HashMap<String, Employee>();
            inputEmployeeMap.put("1", new Employee());
            inputEmployeeMap.put("2", new Employee());
            inputEmployeeMap.put("3", new Employee());

            Map<String, Map<String, Employee>> inputClassList = new HashMap<String, Map<String, Employee>>();

            inputClassList.put("class1", inputEmployeeMap);
            inputClassList.put("class2", inputEmployeeMap);

            Map<String, Map<String, Map<String, Employee>>> inputDeptList = new HashMap<String, Map<String, Map<String, Employee>>>();

            inputDeptList.put("ece", inputClassList);
            inputDeptList.put("csc", inputClassList);

            Map<String, Map<String, Map<String, Map<String, Employee>>>> inputCompanyList = new HashMap<String, Map<String, Map<String, Map<String, Employee>>>>();

            inputCompanyList.put("ABCCompany", inputDeptList);

            // parseMap
            parseMultiValueMap(inputCompanyList);

        }

        private static void parseMultiValueMap(
                Map<String, Map<String, Map<String, Map<String, Employee>>>> inputCompanyList) {
            Iterator<Entry<String, Map<String, Map<String, Map<String, Employee>>>>> it = inputCompanyList
                    .entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry companyList = (Map.Entry) it.next();
                System.out.println("Company Name = " + companyList.getKey()); // company
                                                                                // name
                Map deptList = (Map) companyList.getValue(); // department list

                // iterator for department List.
                Iterator companyListIterator = deptList.entrySet().iterator();
                while (companyListIterator.hasNext()) {
                    Map.Entry departmentList = (Map.Entry) companyListIterator
                            .next();
                    System.out.print(" Dept Name = " + departmentList.getKey()); // department
                    // class map
                    Map classList = (Map) departmentList.getValue();

                    Iterator departmentListIterator = classList.entrySet()
                            .iterator();
                    Map.Entry empNames = (Map.Entry) departmentListIterator.next();
                    // class name
                    System.out.print(" class name = " + empNames.getKey());
                    // employee map
                    Map empNamesMap = (Map) empNames.getValue();

                    Iterator eNamesIt = empNamesMap.entrySet().iterator();
                    while (eNamesIt.hasNext()) {
                        Map.Entry name = (Map.Entry) eNamesIt.next();
                        // prints the emp Id
                        System.out.print(" id " + name.getKey());
                        System.out.print(" name   = " + name.getValue());
                    }
                    System.out.println("\n");
                }
                System.out.println("\n");

            }
        }
    }

Hope it helps.
public class AbcTest {
  public static void main(String[] args) {

    Address address1 = new Address("street1");
    Address address2 = new Address("street2");

    Employee employee1 = new Employee("employee1");
    employee1.getAddresses().add(address1);
    employee1.getAddresses().add(address2);

    Employee employee2 = new Employee("employee2");
    employee2.getAddresses().add(address2);

    Company companyA = new Company("compA");

    companyA.getEmployees().add(employee1);
    companyA.getEmployees().add(employee1); // you can add employee to the list as many times as you want, if you really need this?
    companyA.getEmployees().add(employee2);

    // now to get the employee with give name simly iterate over list of employees

    Iterator<Employee> employeeIt = companyA.getEmployees().iterator();


    Employee wantedEmployee = null;

    while (employeeIt.hasNext() && (wantedEmployee == null)) {
      Employee next = employeeIt.next();

      if (next.getName().equals("employee1")) {
        wantedEmployee = next;
      }
    }

    System.out.println(wantedEmployee);


  }



}


class Company {
  final String name;

  final List<Employee> employees;

  Company(String name) {
    this.name = name;
    this.employees = new ArrayList<>();
  }

  public String getName() {
    return name;
  }

  public List<Employee> getEmployees() {
    return employees;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if ((o == null) || (getClass() != o.getClass())) {
      return false;
    }

    Company company = (Company) o;

    if ((employees != null) ? (!employees.equals(company.employees)) : (company.employees != null)) {
      return false;
    }
    if ((name != null) ? (!name.equals(company.name)) : (company.name != null)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = (name != null) ? name.hashCode() : 0;
    result = (31 * result) + ((employees != null) ? employees.hashCode() : 0);
    return result;
  }
}

class Employee {
  final String name;
  final Set<Address> addresses;

  Employee(String name) {
    this.name = name;
    this.addresses = new HashSet<>();
  }

  public String getName() {
    return name;
  }

  public Set<Address> getAddresses() {
    return addresses;
  }

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

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if ((o == null) || (getClass() != o.getClass())) {
      return false;
    }

    Employee employee = (Employee) o;

    if ((addresses != null) ? (!addresses.equals(employee.addresses)) : (employee.addresses != null)) {
      return false;
    }
    if ((name != null) ? (!name.equals(employee.name)) : (employee.name != null)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result = (name != null) ? name.hashCode() : 0;
    result = (31 * result) + ((addresses != null) ? addresses.hashCode() : 0);
    return result;
  }
}


class Address {
  final String street;

  Address(String street) {
    this.street = street;
  }

  public String getStreet() {
    return street;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if ((o == null) || (getClass() != o.getClass())) {
      return false;
    }

    Address address = (Address) o;

    if ((street != null) ? (!street.equals(address.street)) : (address.street != null)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    return (street != null) ? street.hashCode() : 0;
  }
}