【问题标题】:How to compare a HashMap and Arraylist to find similar values如何比较 HashMap 和 Arraylist 以找到相似的值
【发布时间】:2017-04-08 19:12:10
【问题描述】:

我正在使用这个algorithm 来比较两组值:一个HashMap<Integer,ArrayList<Integer>> 和另一个ArrayList<Integer>。该程序的目标是将 HashMap 中的每个值与 ArrayList 中的值进行比较,并返回相似值的新 HashMap。到目前为止,我的程序运行正常,但它返回错误的结果。

ArrayList 示例

[1.0.1.0.0]

HashMap 示例

  1. [1.0.1.1.0]
  2. [0.1.1.0.0]
  3. [0.1.1.1.0]

结果:

  1. 4
  2. 3
  3. 2

我的计划

int k = 1;
List<Integer> listOperateur = new ArrayList<Integer>();
HashMap<Integer, Integer> sim = new HashMap<Integer, Integer>();

for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) {
    count = 0;

    for (Integer mapValue : e.getValue()) {
        if (mapValue.equals(listOperateur.get(k))) {
            count++;
        }
    }

    sim.put(e.getKey(), count);
    k++;

}

【问题讨论】:

  • 你的问题是什么?
  • 程序在newhashmap中给出了错误的结果
  • 我不太明白这是什么意思。
  • 计数器'count'不计算正确的,它给出了错误的结果,我不知道循环中的问题在哪里
  • 如果我们确切地知道您要达到的目标,那么诊断问题会更容易。您说您想将Map 中的每个值与List 中的值进行比较,但我不明白这是什么意思。

标签: java arraylist hashmap


【解决方案1】:
 System.out.println("HASHMAP RESULT:");
    LinkedHashMap<Integer, ArrayList<Integer>> hmm = new LinkedHashMap<>();
    for (Entry<Integer, List<String>> ee : hm.entrySet()) {
        Integer key = ee.getKey();

        List<String> values = ee.getValue();
        List<Integer> list5 = new ArrayList<>();
        for (String temp : global) {

            list5.add(values.contains(temp) ? 1 : 0);

        }
        hmm.put(key, (ArrayList<Integer>) list5);

    }

    //nouvelle list operateur
    List<Integer> list6 = new ArrayList<Integer>();
    System.out.println("liste operateur");
    List<Integer> listOperateur = new ArrayList<Integer>();
    listOperateur.add(1);
    listOperateur.add(0);
    listOperateur.add(0);
    listOperateur.add(0);
    Iterator iter = listOperateur.iterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }
    //calcule similarité

    System.out.println("HASHMAP SIMILIRATE RESULT:");
    HashMap<Integer, Integer> sim = new HashMap<Integer, Integer>();

    int count;
    int k = 0;


    for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) {
        //if (e.getValue() != null) {
        count = 0;
        //you can move this part to another method to avoid deep nested code
        for (Integer mapValue : e.getValue()) {

            if (mapValue.equals(listOperateur.get(k))) {
                count++;

            }
        }
        sim.put(e.getKey(), count);
        k++;

    }

【讨论】:

    【解决方案2】:

    我相信您正试图依赖将对象插入到 HashMap 中的顺序,这是不明智的,因为没有保留顺序。相反,请尝试将hmm 的类型从HashMap 更改为LinkedHashMap,看看是否能解决您的问题。

    编辑:另外,您可能应该将 k 初始化为 0 而不是 1。

    【讨论】:

    • 它给了我同样的结果
    • @FzKaddour 您可以在 OP 中发布所有更新的代码吗?包括你在哪里初始化和添加元素到hmm
    • 我现在发布了
    • @FzKaddour 现在看来您还有另一个名为hmMap。尝试将其设为LinkedHashMap。如果它不能解决您的问题,那么我一定无法理解您想要实现的目标。
    • 我已经尝试过了,它给了我同样的结果谢谢你的帮助
    猜你喜欢
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2015-01-19
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多