【问题标题】:Convert 2D arraylist to hashmap in Java在Java中将二维数组列表转换为哈希图
【发布时间】:2016-10-18 19:06:17
【问题描述】:

我有一个关于在 java 中将 2d arraylist 转换为 hashmap 的小问题。读取为 2d arraylist 后,我​​的数据集如下所示:

0 1

0 2

1 2

1 3

第一列代表id,第二列代表item。我想在java中使用hashmap创建频繁项集,输出应该是这样的

1 0

2 0 1

3 1

我使用这些代码,但我遇到了一些问题:

HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
for(Integer elem : data){
        map.put(elem[1], elem[0]);
}

data 是我的二维数组列表。

错误信息说

incompatible types: ArrayList<Integer> cannot be converted to Integer
    for(Integer elem : data){
                       ^

任何帮助将不胜感激!

【问题讨论】:

    标签: java arraylist hashmap


    【解决方案1】:

    你是这样的:

    List<List<Integer>> inputData = ...
    
    Map<Integer, List<Integer>> dataAsMap = new HashMap<>();
    for(List<Integer> row : data){
      Integer id = row.get(0);
      Integer item = row.get(1);
      List<Integer> rowInMap = dataAsMap.get(item);
      if (rowInMap == null) {
        rowInMap = new ArrayList<>(); 
        dataAsMap.put(item, rowInMap);
      }
      rowInMap.add(id);
    }
    

    一些注意事项:

    1. 您应该使用 接口 类型 List、Map ... 作为类型(您只在创建新对象时指定特定的 impl 类型,例如 HashMap!)
    2. 您的问题是:当在列表列表上使用 for-each 时(就像您所做的那样)...您不会得到单个“单元格”相反,您会得到 Lists [迭代列表列表...结果为一个每次迭代都列出!]

    所以,剩下的就是获取内部 List 的元素,并将它们推送到 Map 中。另一部分要注意:您要创建 List 对象的 Map。并且还需要创建那些 List 对象!

    ( 我没有通过编译器运行以上代码,所以要注意拼写错误,但总的来说它应该告诉你你需要知道什么。如果你不明白代码在做什么,我建议添加 println语句或在调试器中运行)

    【讨论】:

    • 非常感谢!我真的很感激!
    【解决方案2】:

    这是一种简单的方法:

    1. 拍下Map&lt;Integer, List&lt;Integer&gt;&gt;
    2. 迭代您的 Arraylist。
    3. 查看地图中是否已经存在密钥。如果存在键,则检索列表并将值添加到列表中,否则使用该值创建一个新列表。

    计划:

    class FooBar {
        public static void main (String[] args) throws Exception {
            int[][] data = {{0,1}, {0,2}, {1,2}, {1,3}};
            Map<Integer, List<Integer>> myMap = new HashMap<>();
    
            for(int i = 0; i < 4; i++) {
                List<Integer> values = myMap.containsKey(data[i][0]) ?
                                       myMap.get(data[i][0]) : new ArrayList<>();
                values.add(data[i][1]);
                myMap.put(data[i][0], values);
            }
    
            System.out.println(myMap);
        }
    }
    

    输出:

    {0=[1, 2], 1=[2, 3]}
    

    这只是为了说明基本方法。您显然可以修改它以满足您的需要。例如,您可以使用String 而不是List&lt;Integer&gt;,并选择将值附加到String 而不是将其添加到List

    编辑:

    这是一个以List&lt;List&lt;Integer&gt;&gt; 为输入的示例程序。这里我假设这个列表的名称为input

    计划:

    class FooBar {
        public static void main (String[] args) throws Exception {
            /* Input Data */
            List<List<Integer>> input = new ArrayList<>();
            input.add(new ArrayList<Integer>(){{add(0); add(1);}});
            input.add(new ArrayList<Integer>(){{add(0); add(2);}});
            input.add(new ArrayList<Integer>(){{add(1); add(2);}});
            input.add(new ArrayList<Integer>(){{add(1); add(3);}});
    
            Map<Integer, List<Integer>> myMap = new HashMap<>();
            for(int i = 0; i < input.size(); i++) {
                List<Integer> values = myMap.containsKey(input.get(i).get(0)) ?
                                       myMap.get(input.get(i).get(0)) : new ArrayList<>();
                values.add(input.get(i).get(1));
                myMap.put(input.get(i).get(0), values);
            }
    
            System.out.println(myMap);
        }
    }
    

    输出:

    {0=[1, 2], 1=[2, 3]}
    

    【讨论】:

    • 非常感谢!但是如果输入数据是arraylist呢?
    • @lixx3013 如果它是一个ArrayList,那么你将不得不迭代它而不是int[][]。看看修改后的答案。
    • @lixx3013 如果您认为这解决了您的问题,请接受答案。
    猜你喜欢
    • 2016-05-08
    • 2011-01-16
    • 1970-01-01
    • 2017-11-22
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    相关资源
    最近更新 更多