使用多个HashMaps在Java中检索值

时间:2014-02-13 06:16:50

标签: java collections hashmap

考虑两个HashMap。第一个分别包含产品名称和产品类别代码作为键和值。第二个HashMap包含产品名称和销售的单位。我需要编写一个Java函数,它接受两个哈希映射并返回每个类别中具有最高销售数量的产品名称。

Input1 :{“lux”:”soap”,”colgate”:”paste”, ”pears”:”soap”,”sony”:”electronics”,”samsung”:”electronics”}
Input 2:{“lux”:1000,”colgate”:500,”pears”:2000,”sony”:100,” samsung”,600}
Output: {“pears”,”colgate”,”samsung”}

2 个答案:

答案 0 :(得分:0)

基本上,我会拿着一张从类别到最受欢迎的物品及其柜台的地图。您可以使用两个单独的Map执行此操作,使用快速且脏的类来保存信息,或使用Apache Commons Lang's Pair class之类的内容。如果不依赖任何第三方,我只需要定义一个这样的简单类:

public class Item {
    String name;
    int amount;

    // constructor from name, amount

    // getters and setters
}

现在,您可以遍历销售地图,只保存每个类别中最受欢迎的项目:

public Set<String> getPopularItems
    (Map<String, String> itemCategories, 
     Map<String, Integer> itemSales) {

    Map<String, Item> result = new HashMap<String, Item();

    for (Map.Entry<String, Integer> salesEntry: itemSales) {
        String itemName = itemSales.getKey();
        int itemAmount = itemSales.getValue();

        String category = itemCategories.get(itemName);

        if (result.contiansKey(category)) {
            int currAmount = result.get(category).getAmount();
            if (itemAmount > currAmount) {
                result.put (category, new Item (itemName, itemAmount));
            }
        } else {
            result.put (category, new Item (itemName, itemAmount));
        }
    }

    return result.keySet();
}

请注意,这是一种简化的实现,不会处理格式错误的输入(例如,出现在一个地图中但不出现在另一个地图中的类别),或者是边缘情况(例如,两个具有相同销售额的项目)。这样做是为了清楚起见 - 我相信如果需要你可以在以后添加这些。

答案 1 :(得分:0)

@Arjun:答案代码如下:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MultipleHashMapAccessDemo {

    protected Map<String, String> getProductCategoryMap() {
        Map<String, String> productCategoryMap = new HashMap<String, String>();

        productCategoryMap.put("Lux", "Soap");
        productCategoryMap.put("Pears", "Soap");
        productCategoryMap.put("Dove", "Soap");
        productCategoryMap.put("Colgate", "Paste");
        productCategoryMap.put("Babul", "Paste");
        productCategoryMap.put("Vico", "Paste");

        return productCategoryMap;
    }

    protected Map<String, Integer> getProductUnitsSoldMap() {
        Map<String, Integer> productUnitsSoldMap = new HashMap<String, Integer>();

        productUnitsSoldMap.put("Lux", 1000);
        productUnitsSoldMap.put("Pears", 3000);
        productUnitsSoldMap.put("Dove", 3010);
        productUnitsSoldMap.put("Colgate", 50);
        productUnitsSoldMap.put("Babul", 45);
        productUnitsSoldMap.put("Vico", 80);

        return productUnitsSoldMap;
    }

    protected Map<String, String> getExpectedProductCategoryMap(
            Map<String, String> productCategoryMap,
            Map<String, Integer> productUnitsSoldMap) {

        Map<String, String> expectedProductCategoryMap = new HashMap<String, String>();
        Set<String> categortSet = new HashSet<String>();

        Iterator iterator = productCategoryMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> mEntry = (Map.Entry) iterator.next();
            categortSet.add(mEntry.getValue());
        }

        for (String category : categortSet) {
            int tempUnits = 0;
            String desiredProductName = null;

            for (Object object : productUnitsSoldMap.entrySet()) {
                Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) object;

                String product = entry.getKey();
                if (category.equals(productCategoryMap.get(product))) {
                    if (tempUnits < entry.getValue()) {
                        tempUnits = entry.getValue();
                        desiredProductName = product;
                    }
                }
            }
            expectedProductCategoryMap.put(category, desiredProductName);
        }
        return expectedProductCategoryMap;
    }

    public static void main(String... strings) {

        MultipleHashMapAccessDemo accessDemo = new MultipleHashMapAccessDemo();
        Map<String, String> productCategoryMap = accessDemo
                .getProductCategoryMap();
        Map<String, Integer> productUnitsSoldMap = accessDemo
                .getProductUnitsSoldMap();

        Map<String, String> expectedProductCategoryMap = accessDemo
                .getExpectedProductCategoryMap(productCategoryMap,
                        productUnitsSoldMap);

        for (Object object : expectedProductCategoryMap.entrySet()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) object;

            System.out.print("Category name is : " + entry.getValue());
            System.out.println(" And Product name is : " + entry.getKey());
        }
    }
}