Multimap Examples using Java Collections, Apache Collections and Guava Collections
Single Key with Multiple Values using Hashmap with List using Java Collections
This post will discuss Multimap and how this could be done using three different libraries – Google Guava Collections, Apache Collections and using Java’s own implementation which contains both a Map and a List.
What is a Multimap
A Multimap also called a Multihash is a variation of a Map in which multiple values or objects are associated with a single key.
Using Java JDK to Implement a Viable Solution
When writing my implementation I decided to have a method insertAssociate which has all the logic enabling you to add associates to the elements in the list which are mapped with a particular key, in my case, I am using the location code of each associate.
Using Java Collections to implement MultiMap
package com.avaldes; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.avaldes.model.Associate; public class JavaMultiMapValueExample { public static void main(String[] args) { Logger logger = LoggerFactory .getLogger(JavaMultiMapValueExample.class); Map<String, List<Associate>> multiMap = new HashMap<String, List<Associate>>(); logger.info("Building all the associates needed for MultiMap..."); Associate associate1 = new Associate("1872982", "Amaury", "Valdes", "IT", "777", "IBM", "S"); Associate associate2 = new Associate("2873930", "John", "Smith", "SALES", "777", "IBM", "S"); Associate associate3 = new Associate("4985095", "Davis", "Connor", "PAYROLL", "892", "Oracle", "C"); Associate associate4 = new Associate("1119820", "Michael", "Lipari", "SECURITY", "892", "Oracle", "C"); Associate associate5 = new Associate("3874984", "James", "Silver", "OPERATIONS", "627", "Pivital", "S"); Associate associate6 = new Associate("4453211", "Jonathan", "Daniels", "IT", "627", "Pivital", "S"); Associate associate7 = new Associate("9984748", "Eric", "Hamlin", "CS", "777", "IBM", "S"); insertAssociate(associate1, multiMap); insertAssociate(associate2, multiMap); insertAssociate(associate3, multiMap); insertAssociate(associate4, multiMap); insertAssociate(associate5, multiMap); insertAssociate(associate6, multiMap); insertAssociate(associate7, multiMap); // Let's get all the keys and loop through them logger.info("Getting all keys from the MultiMap..."); Set<String> keys = multiMap.keySet(); for (String key : keys) { logger.info("Key: [" + key + "], Values = " + multiMap.get(key)); } } public static void insertAssociate(Associate associate, Map<String, List<Associate>> multiMap) { List<Associate> list; if (multiMap.containsKey(associate.getLocation_code())) { list = multiMap.get(associate.getLocation_code()); } else { list = new ArrayList<Associate>(); } list.add(associate); multiMap.put(associate.getLocation_code(), list); } }
Output from Java JDK MultiMap Implementation
Building all the associates needed for MultiMap... Getting all keys from the MultiMap... Key: [777], Values = [Associate [uid=1872982, employee_type=S, first_name=Amaury, last_name=Valdes, department=IT, location_code=777, company=IBM], Associate [uid=2873930, employee_type=S, first_name=John, last_name=Smith, department=SALES, location_code=777, company=IBM], Associate [uid=9984748, employee_type=S, first_name=Eric, last_name=Hamlin, department=CS, location_code=777, company=IBM]] Key: [627], Values = [Associate [uid=3874984, employee_type=S, first_name=James, last_name=Silver, department=OPERATIONS, location_code=627, company=Pivotal], Associate [uid=4453211, employee_type=S, first_name=Jonathan, last_name=Daniels, department=IT, location_code=627, company=Pivotal]] Key: [892], Values = [Associate [uid=4985095, employee_type=C, first_name=Davis, last_name=Connor, department=PAYROLL, location_code=892, company=Oracle], Associate [uid=1119820, employee_type=C, first_name=Michael, last_name=Lipari, department=SECURITY, location_code=892, company=Oracle]]
Using Google Guava Collections to implement MultiMap
package com.avaldes; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.avaldes.model.Associate; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; public class GuavaMultiMapValueExample { public static void main(String[] args) { Logger logger = LoggerFactory .getLogger(GuavaMultiMapValueExample.class); Multimap<String, Associate> multiMap = ArrayListMultimap.create(); logger.info("Building all the associates needed for MultiMap..."); Associate associate1 = new Associate("1872982", "Amaury", "Valdes", "IT", "777", "IBM", "S"); Associate associate2 = new Associate("2873930", "John", "Smith", "SALES", "777", "IBM", "S"); Associate associate3 = new Associate("4985095", "Davis", "Connor", "PAYROLL", "892", "Oracle", "C"); Associate associate4 = new Associate("1119820", "Michael", "Lipari", "SECURITY", "892", "Oracle", "C"); Associate associate5 = new Associate("3874984", "James", "Silver", "OPERATIONS", "627", "Pivital", "S"); Associate associate6 = new Associate("4453211", "Jonathan", "Daniels", "IT", "627", "Pivital", "S"); Associate associate7 = new Associate("9984748", "Eric", "Hamlin", "CS", "777", "IBM", "S"); multiMap.put(associate1.getLocation_code(), associate1); multiMap.put(associate2.getLocation_code(), associate2); multiMap.put(associate3.getLocation_code(), associate3); multiMap.put(associate4.getLocation_code(), associate4); multiMap.put(associate5.getLocation_code(), associate5); multiMap.put(associate6.getLocation_code(), associate6); multiMap.put(associate7.getLocation_code(), associate7); // Let's get all the keys and loop through them logger.info("Getting all keys from the MultiMap..."); Set<String> keys = multiMap.keySet(); for (String key : keys) { logger.info("Key: [" + key + "], Values = " + multiMap.get(key)); } } }
Output from Google Guava MultiMap Implementation
Building all the associates needed for MultiMap... Getting all keys from the MultiMap... Key: [777], Values = [Associate [uid=1872982, employee_type=S, first_name=Amaury, last_name=Valdes, department=IT, location_code=777, company=IBM], Associate [uid=2873930, employee_type=S, first_name=John, last_name=Smith, department=SALES, location_code=777, company=IBM], Associate [uid=9984748, employee_type=S, first_name=Eric, last_name=Hamlin, department=CS, location_code=777, company=IBM]] Key: [627], Values = [Associate [uid=3874984, employee_type=S, first_name=James, last_name=Silver, department=OPERATIONS, location_code=627, company=Pivotal], Associate [uid=4453211, employee_type=S, first_name=Jonathan, last_name=Daniels, department=IT, location_code=627, company=Pivotal]] Key: [892], Values = [Associate [uid=4985095, employee_type=C, first_name=Davis, last_name=Connor, department=PAYROLL, location_code=892, company=Oracle], Associate [uid=1119820, employee_type=C, first_name=Michael, last_name=Lipari, department=SECURITY, location_code=892, company=Oracle]]
Using Apache Collections to implement MultiMap
package com.avaldes; import java.util.Set; import org.apache.commons.collections.MultiMap; import org.apache.commons.collections.map.MultiValueMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.avaldes.model.Associate; public class ApacheMultiMapValueExample { public static void main(String[] args) { Logger logger = LoggerFactory .getLogger(ApacheMultiMapValueExample.class); MultiMap multiMap = new MultiValueMap(); logger.info("Building all the associates needed for MultiMap..."); Associate associate1 = new Associate("1872982", "Amaury", "Valdes", "IT", "777", "IBM", "S"); Associate associate2 = new Associate("2873930", "John", "Smith", "SALES", "777", "IBM", "S"); Associate associate3 = new Associate("4985095", "Davis", "Connor", "PAYROLL", "892", "Oracle", "C"); Associate associate4 = new Associate("1119820", "Michael", "Lipari", "SECURITY", "892", "Oracle", "C"); Associate associate5 = new Associate("3874984", "James", "Silver", "OPERATIONS", "627", "Pivital", "S"); Associate associate6 = new Associate("4453211", "Jonathan", "Daniels", "IT", "627", "Pivital", "S"); Associate associate7 = new Associate("9984748", "Eric", "Hamlin", "CS", "777", "IBM", "S"); multiMap.put(associate1.getLocation_code(), associate1); multiMap.put(associate2.getLocation_code(), associate2); multiMap.put(associate3.getLocation_code(), associate3); multiMap.put(associate4.getLocation_code(), associate4); multiMap.put(associate5.getLocation_code(), associate5); multiMap.put(associate6.getLocation_code(), associate6); multiMap.put(associate7.getLocation_code(), associate7); // Let's get all the keys and loop through them logger.info("Getting all keys from the MultiMap..."); Set<String> keys = multiMap.keySet(); for (String key : keys) { logger.info("Key: [" + key + "], Values = " + multiMap.get(key)); } } }
Output from Apache Collections MultiMap Implementation
Building all the associates needed for MultiMap... Getting all keys from the MultiMap... Key: [777], Values = [Associate [uid=1872982, employee_type=S, first_name=Amaury, last_name=Valdes, department=IT, location_code=777, company=IBM], Associate [uid=2873930, employee_type=S, first_name=John, last_name=Smith, department=SALES, location_code=777, company=IBM], Associate [uid=9984748, employee_type=S, first_name=Eric, last_name=Hamlin, department=CS, location_code=777, company=IBM]] Key: [627], Values = [Associate [uid=3874984, employee_type=S, first_name=James, last_name=Silver, department=OPERATIONS, location_code=627, company=Pivotal], Associate [uid=4453211, employee_type=S, first_name=Jonathan, last_name=Daniels, department=IT, location_code=627, company=Pivotal]] Key: [892], Values = [Associate [uid=4985095, employee_type=C, first_name=Davis, last_name=Connor, department=PAYROLL, location_code=892, company=Oracle], Associate [uid=1119820, employee_type=C, first_name=Michael, last_name=Lipari, department=SECURITY, location_code=892, company=Oracle]]
Associate Model (Associate.java)
package com.avaldes.model; public class Associate { private String uid; private String employee_type; private String first_name; private String last_name; private String department; private String location_code; private String company; public Associate(String uid, String first_name, String last_name, String department, String location_code, String company, String employee_type ) { this.uid = uid; this.first_name = first_name; this.last_name = last_name; this.department = department; this.location_code = location_code; this.company = company; this.employee_type = employee_type; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getEmployee_type() { return employee_type; } public void setEmployee_type(String employee_type) { this.employee_type = employee_type; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getLocation_code() { return location_code; } public void setLocation_code(String location_code) { this.location_code = location_code; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } @Override public String toString() { return "Associate [uid=" + uid + ", employee_type=" + employee_type + ", first_name=" + first_name + ", last_name=" + last_name + ", department=" + department + ", location_code=" + location_code + ", company=" + company + "]"; } }
Leave a Reply