1

我正在为一家小型企业创建一些图形软件。他们给了我一份他们去年的销售报告,在这份报告中,它显示了每个特定销售的“时间戳”和项目或“sku”,但没有特定的顺序。我想弄清楚在某一天每件商品的销量。我知道我想使用地图,但我不太熟悉在这种情况下如何使用它。如果 sku 在特定日期售出 0 件商品,我还需要地图返回 0。这很难,因为我不知道报告上会有多少个sku,而且报告没有特定的顺序。

例如,报告可以显示:

sku="AKD123"; timestamp="2012-02-01";
sku="AKD123"; timestamp="2012-04-14";
sku="REN134"; timestamp="2012-02-01";
sku="PIK383"; timestamp="2012-10-07";
sku="REN134"; timestamp="2012-02-01";
sku="REN134"; timestamp="2012-02-01";
sku="PIK383"; timestamp="2012-03-01";
sku="REN134"; timestamp="2012-02-01";

我怎么想有一张地图,我可以检查“2012-02-01”销售了多少“REN134”,希望得到 4 的回报。

我希望我说得足够清楚。谢谢!

4

5 回答 5

3

Map 不能有重复的键。您需要做的是有一个HashMap<String, ArrayList<String>>或类似的东西,其中键是 SKU 编号,值是所有日期的列表,然后您可以将其用于进一步排序和处理以计算出现次数。

于 2013-03-07T20:47:39.497 回答
3

您需要两张地图,一张用于匹配sku某个日期和计数,另一张将每个日期映射到计数......试试这个......

final static String skus[] = {
    "AKD123",
    "AKD123",
    "REN134",
    "PIK383",
    "REN134",
    "REN134",
    "PIK383",
    "REN134"
};

final static String timestamp[] = {
    "2012-02-01",
    "2012-04-14",
    "2012-02-01",
    "2012-10-07",
    "2012-02-01",
    "2012-02-01",
    "2012-03-01",
    "2012-02-01"
};


@Test
public void countSkusPerDay() {
    Map<String, Map<String, Integer>> countMap = new HashMap<>();
    for(int i = 0; i < skus.length; i++) {
        String sku = skus[i];
        String date = timestamp[i];
        Map<String, Integer> countPerDateMap = countMap.get(sku);
        if(countPerDateMap == null) {
            countPerDateMap = new HashMap<>();
            countMap.put(sku, countPerDateMap);
        }
        Integer count = countPerDateMap.get(date);
        if(count == null) {
            countPerDateMap.put(date, 1);
        } else {
            countPerDateMap.put(date, count.intValue() + 1);
        }
    }

    for(Map.Entry<String, Map<String, Integer>> e : countMap.entrySet()) {
        System.out.println("sku " + e.getKey() + " sold in " + e.getValue().size() + " day(s) " + e.getValue());
    }
}

输出是

sku REN134 sold in 1 day(s) {2012-02-01=4}
sku PIK383 sold in 2 day(s) {2012-10-07=1, 2012-03-01=1}
sku AKD123 sold in 2 day(s) {2012-02-01=1, 2012-04-14=1}
于 2013-03-07T21:11:44.640 回答
1

考虑这段代码(我已经添加了您的示例数据)......它利用了 Map 不能有重复键的事实:

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

public class TEST
{
  public static void main(String[] args)
  {
/*
sku="AKD123"; timestamp="2012-02-01";
sku="AKD123"; timestamp="2012-04-14";
sku="REN134"; timestamp="2012-02-01";
sku="PIK383"; timestamp="2012-10-07";
sku="REN134"; timestamp="2012-02-01";
sku="REN134"; timestamp="2012-02-01";
sku="PIK383"; timestamp="2012-03-01";
sku="REN134"; timestamp="2012-02-01";

    Transform into this array:
*/
    String[] inputValues = new String[]
    { "AKD123|2012-02-01", "AKD123|2012-04-14", "REN134|2012-02-01", "PIK383|2012-10-07", "REN134|2012-02-01", "REN134|2012-02-01",
        "PIK383|2012-03-01", "REN134|2012-02-01" };

    // That is your map
    Map<String, Integer> mapCouter = new HashMap<String, Integer>();
    // now iterate though all SKU sales
    for (String key : inputValues)
    {
      Integer count = mapCouter.get(key);
      if (count == null)
      {
        // not found... lets init.
        count = 0; // using java autoboxing.
      }
      mapCouter.put(key, ++count); // incrementing by 1 before storing in the Map
    }
    System.out.println("Report:");
    Set<String> keys = mapCouter.keySet();
    for (String key : keys)
    {
      System.out.println("key: " + key + " has " + mapCouter.get(key) + " occurences.");
    }
  }
}

它产生以下输出:

Report:
key: REN134|2012-02-01 has 4 occurences.
key: AKD123|2012-04-14 has 1 occurences.
key: PIK383|2012-10-07 has 1 occurences.
key: AKD123|2012-02-01 has 1 occurences.
key: PIK383|2012-03-01 has 1 occurences.

注意:您需要解析输入文件(因为它可能是大数据)并使用所示的“地图计数器”技术。

于 2013-03-07T21:03:22.763 回答
0

创建您自己的Item类来封装skutimestamp字段。实现hashCodeandequals方法,以便稍后 aHashMap可以正常工作。

创建您的地图。

Map<Item, Integer> salesMap = new HashMap<Item, Integer>();

您需要使用 0 的值初始化地图,以便稍后您可以了解哪些商品在一天内没有销售。

从报告中读取数据并创建您的Item对象。如果地图中还不存在,则将其存储在地图中,值为 1。如果存在,则取存储的值,加 1,并存储新值。

于 2013-03-07T20:50:52.067 回答
0

有一张Guava Multisets的地图怎么样Map<String,Multiset<String>> itemToDatesSoldMap = new HashMap<>();。这将允许我们做类似的事情

public int countSalesOfItemOnDate(String sku, String timestamp){
    Multiset<String> timestampMultiset = itemToDatesSoldMap.get(sku);
    if(timestampMultiset == null){
        return 0;
    }
    return timestampMultiset.count(timestamp);
}

countSalesOfItemOnDate("REN134","2012-02-01");
于 2013-03-07T21:14:10.020 回答