我有一个MultiMap假设公司,它将String作为键,另一个MultiMap将Employee视为值。 Employee Multimap将String作为键,将另一个multimap作为值。我的问题是如何检索和迭代存储在multimap中的multimap?我正在使用Apache常见的MultiMap。
示例:compMap有2家公司。 CompA和CompB 每个公司有10名员工。(员工可以多次出现因此使用multimap)
Coll包含来自compA的员工多图,但是如何从员工多图中检索特定员工(如果他出现4次)?
代码:
if (!compMap.isEmpty()){
Set compSet = compMap.keySet( );
Iterator compIterator = compSet.iterator();
while( compIterator.hasNext( ) ) {
comp= compIterator.next().toString();
Collection coll = (Collection) compMap.get(comp);
.....
}
答案 0 :(得分:0)
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.
答案 1 :(得分:0)
您遇到的问题是由于一名员工不应在一个集合中出现两次而导致的。如果在MultiValueMap中的同一个键下存储两个值,则这些值是不同的值(尽管它们可以通过相同的键访问)。
您应该将数据模型迁移到更多面向对象的模型,而不要使用位于另一个密钥库容器内的密钥库容器(映射到地图内)。
仅使用Set / List考虑以下模型。基于equals / hashcode区分值。如果您使用List,您可以在公司内部拥有许多员工(如果您确实需要,他们可以使用相同的名称)。为什么使用MultiValueMap使其复杂化?
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;
}
}