Apache Common MultiValueMap iteration
I have a MultiMap, suppose a company that has String as key and another MultiMap assumes Employee as value. Employee Multimap has String as key and another multimap as value. My question is, how do I retrieve and iterate over the multimap stored inside the multimap? I am using Apache common MultiMap.
Example: compMap has 2 companies. CompA and CompB each company has 10 employees. (An employee can appear more than once using a multimap)
Coll contains the multimap from compA, but how can I get a specific employee (if it appears 4 times) from the employee's multimap?
code:
if (!compMap.isEmpty()){
Set compSet = compMap.keySet( );
Iterator compIterator = compSet.iterator();
while( compIterator.hasNext( ) ) {
comp= compIterator.next().toString();
Collection coll = (Collection) compMap.get(comp);
.....
}
The problem you are having is caused by the fact that one employee should not appear twice in the same collection. If you are storing two values ββunder the same key in a MultiValueMap than the thesis of different values ββ(although they are accessed by the same key).
You should migrate your data model to a more object oriented model and not use a key storage container located inside another key store container (map inside map).
Consider the following model using only Set / List. The values ββare differentiated based on equals / hashcode. If you use List, you can have many employees within the company (they can have the same name if you really need it). Why are you complicating it with 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;
}
}
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.