搜索arrayLists的maptree

时间:2019-03-23 01:01:39

标签: java hadoop arraylist treemap

我有一个arrayLists的树状图。我需要检查每个列表中的匹配元素。我的代码可以正常工作,只不过它仅针对数组<=本身检查数组。其他阵列运行良好。我以为这是我所缺少的愚蠢错误,但是..

            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));

我仍然需要更正密钥,这很容易,并且不会以任何一种方式在最终程序中使用。

我应该得到 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

1 个答案:

答案 0 :(得分:2)

我对您的某些变量和数据定义有些不确定,因为这两个发布之间存在一些差异。但是,我创建了一个独立的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

    }

}