重构HashMap的最快方法

我有一个HashMap,它将公司映射到他们销售的产品的ArrayList,如下所示:

thiscompany --> [productA, productB...] thatcompany --> [productC, productA...] 

因此,在给定特定公司的情况下生成产品列表非常容易。 请注意,多家公司可能会销售相同的产品。 问题是,鉴于特定产品,我还需要找到所有销售它的公司。 很快。 这种查找可能会发生一次或多次。 我想知道提供此function的最有效方法。

目前,我通过迭代每个ArrayList并将每个产品映射到其供应商来生成新的数据结构。 这很昂贵,因为我必须检查我正在创建的HashMap是否每次添加之前都包含该产品作为键,而且它需要我获取每个ArrayList,添加新的供应商,删除旧的ArrayList然后映射每个条目的新的。 我根本无法看到更快的方式,也许有人可以提供一些见解?

如何将ArrayList更改为HashSet。

 List findCompanies(Map> companyToProducts, String product) { List companies = new ArrayList(); for (Map.Entry> entry : companyToProducts) { Set products = entry.getValue(); if (products.contains(product)) { companies.add(entry.getKey()); } } return companies; } 

另一种常见的方法是在数据库中使用一个表,其中包含产品列和公司列,然后执行以下操作:

 select distinct company from companyToProduct where product = 'cheese'; 

为什么不在为公司构建产品地图的同时创建销售产品的公司地图

 Map> companiesByProduct Map> productsByCompany public void add(Company company, Product product) { Set companies = companiesByProduct.get(product); if (companies==null) { companies = new HashSet(); companiesByProduct.put(product, companies); } companies.add(company); // do the same for Set products = productsByCompany.get(product); .... 

或者从您可以使用的服务器接收的地图创建新的公司产品地图(您可能需要根据原始地图的确切类型进行调整):

 for (Company company : originalMap.keySet()) { for (Product product : originalMap.get(company)) { Set companies = companiesByProduct.get(product); if (companies==null) { companies = new HashSet(); companiesByProduct.put(product, companies); } companies.add(company); } } 

试试MultiMaps.invertFrom ,

例:

  Multimap map = HashMultimap.create(); map.put("a", 3); map.put("a", 4); map.put("a", 5); map.put("b", 5); map.put("b", 3); map.put("b", 6); Multimap mapInverse=HashMultimap.create(); Multimaps.invertFrom(map, mapInverse); System.out.println(mapInverse); 

输出:

 {3=[b, a], 4=[a], 5=[b, a], 6=[b]} 

替代方案:

在这里,我正在创建一个2D布尔数组,代表公司及其产品,以便于查找。

 import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import com.google.common.collect.TreeMultimap; public class ProductCompanyMap { private Multimap companyProducts = TreeMultimap.create(); private boolean[][] ProductCompanyTable; private Map productIndexMap = new HashMap();; private Map companyIndexMap = new HashMap(); private String[] productArray; private String[] companyArray;; { companyProducts.put("Britania","Biscuts"); companyProducts.put("Britania","Soap"); companyProducts.put("Britania","Cloths"); companyProducts.put("MicroSoft","Software"); companyProducts.put("Birla","Cloths"); companyProducts.put("Birla","Software"); } public ProductCompanyMap(){ Set companyNames=companyProducts.keySet(); Set productNames= Sets.newTreeSet(companyProducts.values()); companyArray = companyNames.toArray(new String[companyNames.size()]); createIndexMap(companyIndexMap, companyArray); productArray = productNames.toArray(new String[productNames.size()]); createIndexMap(productIndexMap,productArray); ProductCompanyTable = new boolean[companyArray.length][productArray.length]; for(int i=0;i map,String[] arr){ for(int i=0;i getProductsOfCompany(String companyName){ List productsOfCompany = new ArrayList(); Integer companyIndex = null; if((companyIndex=companyIndexMap.get(companyName))!=null) { for(int i=0;i getCompanysWithProduct(String productName){ List companysWithProduct = new ArrayList(); Integer productIndex = null; if((productIndex=productIndexMap.get(productName))!=null) { for(int i=0;i 

维护两个数据结构 – 您现在维护的HashMap和另一个将产品映射到公司的数据结构:

 productA --> [thiscompany, thatcompany...] productC --> [thatcompany, othercompany...]