java - 搜索 arrayLists 的映射树

标签 java hadoop arraylist treemap

我有一个数组列表的树状图。我需要检查每个列表中的匹配元素。我的代码有效,除了它只检查数组 <= 本身。其他阵列跟踪​​良好。我想这是我错过的一个愚蠢的错误,但是..

            ArrayList<Integer> currentArray;
            ArrayList<Integer> compareArray;

            //Setting user friend list

              //cycle users "i"
              for(int i = 1; i <= treemap.size(); i++){
                currentArray = treemap.get(i);

                  //cycle user "k"
                    for(int k=1; k <= treemap.size(); k++){
                      //if(i!=k){  Put back in once working
                        compareArray = treemap.get(k);
                        //cylce "k"s movie list
                        for(int l=0; l < compareArray.size(); l++){
                          if(currentArray.contains(compareArray.get(l))){
                            if (treemap2.containsKey(i)){
                              ArrayList<Integer> list3 = treemap2.get(i);
                              list3.add(k);
                              treemap2.remove(i);
                              treemap2.put(i, list3);
                            }
                            if (!treemap2.containsKey(i)){
                              ArrayList<Integer> list4 =  new ArrayList<Integer>();
                              list4.add(k);
                              treemap2.put(i, list4);
                            }
                          }
                        }
                    }
              }
            //Create string of friends
            for(ArrayList<Integer>  x: treemap2.values()){
              str2 = Integer.toString(x.get(0));
              for (int i = 1; i < x.size(); i++) 
              {
                  str2 = str2 + "," + Integer.toString(x.get(i)) ;
              }      
            }

            context.write(key, new Text(str2));

我仍然需要更正 key ,这很容易,而且无论如何都不会在最终程序中使用。

我应该得到 1 1,1,1,2,2 2 1,1,2,2,2 3 2,3,3,3

相反,我得到 1 1,1,1 2 1,1,2,2,2 3 2,3,3,3

无论如何,先谢谢你。 旁注,执行以下操作可以得到我想要的...除了它遗漏了最后一个数组....!!

        //cycle current array "i"
        for(int i = 1; i < treemap.size(); i++){
          currentArray = treemap.get(i);
            //cycle compare array "k"
              for(int k=1; k <= treemap.size(); k++){
                if(i!=k){   //
                  compareArray = treemap.get(k);
                  //cylce array element in compare "l"
                  for(int l=0; l < compareArray.size(); l++){
                    if(currentArray.contains(compareArray.get(l))){
                      if (treemap2.containsKey(i)){
                        ArrayList<String> list3 = treemap2.get(i);
                        list3.add(k+":"+compareArray.get(l));
                        treemap2.remove(i);
                        treemap2.put(i, list3);
                      }
                      if (!treemap2.containsKey(i)){
                        ArrayList<String> list4 =  new ArrayList<String>();
                        list4.add(k+":"+compareArray.get(l));
                        treemap2.put(i, list4);
                      }
                    }
                  }
                }
              }
        }




1   2105
1   1953
1   1339
2   2105
2   1321
2   1339
3   1321
3   1544
3   1222

最佳答案

我不太确定您的一些变量和数据定义,因为这两个帖子之间存在一些差异。但是,我创建了一个独立的 Java 程序来隔离您的算法并使用模拟数据对其进行测试。整个程序在下面,它很小。

这将是我的一般方法,混合使用跟踪和调试器断点,以便能够查看结果并在关键点检查所有变量。

您可以更正我对数据类型所做的任何错误假设。

希望对您有所帮助。

package com.example.hadoop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeMap;

/**
 * I took your original code posting (which differs somewhat from the second
 * snippet) and I converted it into a stand alone Java program with mock test
 * data.
 * 
 * This program would be my starting point to finding the logic error.
 * 
 * My approach was to (1) isolate your algorithm, (2) mock out a minimal set of
 * input data to test just the algorithm, and (3) step through with the debugger
 * and/or use some trace statements.
 * 
 * I made assumptions about variable definitions based on the code and
 * information provided since I could not see all the actual definitions.
 * 
 * Some of your data types differ between your two code fragments so I was a bit
 * unsure about that.
 * 
 * I may be wrong on some of the variable definitions so modify these as needed.
 * 
 * You mentioned that:
 * 
 * Expected result: 1 1,1,1,2,2 2 1,1,2,2,2 3 2,3,3,3
 * 
 * Actual result: 1 1,1,1 2 1,1,2,2,2 3 2,3,3,3
 * 
 * Massively truncated test data: 1 2105 1 1953 1 1339 2 2105 2 1321 2 1339 3
 * 1321 3 1544 3 1222
 */
public class HadoopTestApp {

    /*
     * Mock test variables.
     */
    ArrayList<Integer> datalist1 = new ArrayList<Integer>(Arrays.asList(2105, 1953, 1339));
    ArrayList<Integer> datalist2 = new ArrayList<Integer>(Arrays.asList(2105, 1321, 1339));
    ArrayList<Integer> datalist3 = new ArrayList<Integer>(Arrays.asList(1321, 1544, 1222));
    TreeMap<Integer, ArrayList<Integer>> treemap = new TreeMap<Integer, ArrayList<Integer>>();
    TreeMap<Integer, ArrayList<Integer>> treemap2 = new TreeMap<Integer, ArrayList<Integer>>();
    String str2 = "";

    /**
     * Use the default constructor to complete the initialization of mock test
     * variables.
     */
    public HadoopTestApp() {

        treemap.put(1, datalist1);
        treemap.put(2, datalist2);
        treemap.put(3, datalist3);
    }

    /**
     * Bootstrap the test.
     * 
     * @param args Command line arguments are not currently used.
     */
    public static void main(String[] args) {
        new HadoopTestApp().run(args);
    }

    /**
     * If you prefer to trace variables manually. Or you can set some breakpoints
     * and inspect variables in the debugger.
     */
    public void doTrace(String label, Object o) {
        System.out.print(label + ": ");
        System.out.println(o);
    }

    /**
     * Run the test of Hooch's algorithm.
     * 
     * @param args Command line arguments are not currently used.
     */
    public void run(String[] args) {

        doTrace("treemap", treemap); // NEW

        ArrayList<Integer> currentArray;
        ArrayList<Integer> compareArray;

        // Setting user friend list

        // cycle users "i"
        for (int i = 1; i <= treemap.size(); i++) {
            currentArray = treemap.get(i);

            // cycle user "k"
            for (int k = 1; k <= treemap.size(); k++) {
                // if(i!=k){ Put back in once working
                compareArray = treemap.get(k);
                // cylce "k"s movie list
                for (int l = 0; l < compareArray.size(); l++) {
                    if (currentArray.contains(compareArray.get(l))) {
                        // NEW I set a debugger breakpoint on this line to inspect all variables
                        if (treemap2.containsKey(i)) {
                            ArrayList<Integer> list3 = treemap2.get(i);
                            list3.add(k);
                            treemap2.remove(i);
                            treemap2.put(i, list3);
                            doTrace("contains key, treemap2", treemap2); // NEW
                        }
                        if (!treemap2.containsKey(i)) {
                            ArrayList<Integer> list4 = new ArrayList<Integer>();
                            list4.add(k);
                            treemap2.put(i, list4);
                            doTrace("does not contain key, treemap2", treemap2); // NEW
                        }
                    }
                }
            }
        }

        // Create string of friends
        for (ArrayList<Integer> x : treemap2.values()) {
            str2 = Integer.toString(x.get(0));
            for (int i = 1; i < x.size(); i++) {
                str2 = str2 + "," + Integer.toString(x.get(i));
            }
            doTrace("in loop str2", str2); // NEW
        }

        // context.write(key, new Text(str2));
        doTrace("context.write str2", str2); // NEW

    }

}

关于java - 搜索 arrayLists 的映射树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55309613/

相关文章:

hadoop - RedshiftStorage可用于EMR上的Pig作业?

hadoop - 我对hadoop 2.7感到困惑。运行start-all.sh之后,在jps列表中找不到jobtracker和tasktracker,为什么?

java - 可以使用数组代替 arrayList 在 recyclerView 中显示项目吗?

java - spring-boot 2.3.0.M4 JDK 11 集成NoSuchBeanDefinitionException ...ConfigurationClassPostProcessor, ...ProxyTransactionManagementConfiguration

java - 将 Netbeans 项目转换为 Eclipse 项目

linux - 我怎样才能记忆起 shell 的历史?

java - 使用 foreach 遍历 ArrayList 时的线程安全

java - 在速度模板中同时迭代两个列表

java - 在 JTable 单元格中添加 JLabel 时出现奇怪的行为

Java jtable 允许行选择