搜索ArrayList的Map树

wtzytmuj  于 2021-05-27  发布在  Hadoop
关注(0)|答案(1)|浏览(541)

我有一个数组列表的树形图。我需要检查每个匹配元素的列表。我的代码可以工作,只是它只检查数组本身。其他阵列跟踪良好。我想我错过了一个愚蠢的错误,但是。。

  1. ArrayList<Integer> currentArray;
  2. ArrayList<Integer> compareArray;
  3. //Setting user friend list
  4. //cycle users "i"
  5. for(int i = 1; i <= treemap.size(); i++){
  6. currentArray = treemap.get(i);
  7. //cycle user "k"
  8. for(int k=1; k <= treemap.size(); k++){
  9. //if(i!=k){ Put back in once working
  10. compareArray = treemap.get(k);
  11. //cylce "k"s movie list
  12. for(int l=0; l < compareArray.size(); l++){
  13. if(currentArray.contains(compareArray.get(l))){
  14. if (treemap2.containsKey(i)){
  15. ArrayList<Integer> list3 = treemap2.get(i);
  16. list3.add(k);
  17. treemap2.remove(i);
  18. treemap2.put(i, list3);
  19. }
  20. if (!treemap2.containsKey(i)){
  21. ArrayList<Integer> list4 = new ArrayList<Integer>();
  22. list4.add(k);
  23. treemap2.put(i, list4);
  24. }
  25. }
  26. }
  27. }
  28. }
  29. //Create string of friends
  30. for(ArrayList<Integer> x: treemap2.values()){
  31. str2 = Integer.toString(x.get(0));
  32. for (int i = 1; i < x.size(); i++)
  33. {
  34. str2 = str2 + "," + Integer.toString(x.get(i)) ;
  35. }
  36. }
  37. context.write(key, new Text(str2));

我仍然需要纠正的关键,这是很容易的,而不是在最终的程序中使用的任何方式。
我应该得到11,1,1,2,2,1,1,2,2,2,3,3,3
相反,我得到了11,1,12,1,2,2,2,3,3,3
不管怎样,先谢谢你。旁注,做下面的事情能让我得到我想要的。。。但它漏掉了最后一个数组。。。。!!

  1. //cycle current array "i"
  2. for(int i = 1; i < treemap.size(); i++){
  3. currentArray = treemap.get(i);
  4. //cycle compare array "k"
  5. for(int k=1; k <= treemap.size(); k++){
  6. if(i!=k){ //
  7. compareArray = treemap.get(k);
  8. //cylce array element in compare "l"
  9. for(int l=0; l < compareArray.size(); l++){
  10. if(currentArray.contains(compareArray.get(l))){
  11. if (treemap2.containsKey(i)){
  12. ArrayList<String> list3 = treemap2.get(i);
  13. list3.add(k+":"+compareArray.get(l));
  14. treemap2.remove(i);
  15. treemap2.put(i, list3);
  16. }
  17. if (!treemap2.containsKey(i)){
  18. ArrayList<String> list4 = new ArrayList<String>();
  19. list4.add(k+":"+compareArray.get(l));
  20. treemap2.put(i, list4);
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27. 1 2105
  28. 1 1953
  29. 1 1339
  30. 2 2105
  31. 2 1321
  32. 2 1339
  33. 3 1321
  34. 3 1544
  35. 3 1222
iih3973s

iih3973s1#

我对你的一些变量和数据定义有点不确定,因为这两个帖子之间有些不同。但是,我创建了一个独立的java程序,它隔离了您的算法,并使用模拟数据来测试它。整个程序都在下面,非常小。
这将是我使用跟踪和调试器断点的一般方法,以便能够看到结果并在关键点检查所有变量。
你可以纠正我对数据类型的错误假设。
我希望这有帮助。

  1. package com.example.hadoop;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.TreeMap;
  5. /**
  6. * I took your original code posting (which differs somewhat from the second
  7. * snippet) and I converted it into a stand alone Java program with mock test
  8. * data.
  9. *
  10. * This program would be my starting point to finding the logic error.
  11. *
  12. * My approach was to (1) isolate your algorithm, (2) mock out a minimal set of
  13. * input data to test just the algorithm, and (3) step through with the debugger
  14. * and/or use some trace statements.
  15. *
  16. * I made assumptions about variable definitions based on the code and
  17. * information provided since I could not see all the actual definitions.
  18. *
  19. * Some of your data types differ between your two code fragments so I was a bit
  20. * unsure about that.
  21. *
  22. * I may be wrong on some of the variable definitions so modify these as needed.
  23. *
  24. * You mentioned that:
  25. *
  26. * Expected result: 1 1,1,1,2,2 2 1,1,2,2,2 3 2,3,3,3
  27. *
  28. * Actual result: 1 1,1,1 2 1,1,2,2,2 3 2,3,3,3
  29. *
  30. * Massively truncated test data: 1 2105 1 1953 1 1339 2 2105 2 1321 2 1339 3
  31. * 1321 3 1544 3 1222
  32. */
  33. public class HadoopTestApp {
  34. /*
  35. * Mock test variables.
  36. */
  37. ArrayList<Integer> datalist1 = new ArrayList<Integer>(Arrays.asList(2105, 1953, 1339));
  38. ArrayList<Integer> datalist2 = new ArrayList<Integer>(Arrays.asList(2105, 1321, 1339));
  39. ArrayList<Integer> datalist3 = new ArrayList<Integer>(Arrays.asList(1321, 1544, 1222));
  40. TreeMap<Integer, ArrayList<Integer>> treemap = new TreeMap<Integer, ArrayList<Integer>>();
  41. TreeMap<Integer, ArrayList<Integer>> treemap2 = new TreeMap<Integer, ArrayList<Integer>>();
  42. String str2 = "";
  43. /**
  44. * Use the default constructor to complete the initialization of mock test
  45. * variables.
  46. */
  47. public HadoopTestApp() {
  48. treemap.put(1, datalist1);
  49. treemap.put(2, datalist2);
  50. treemap.put(3, datalist3);
  51. }
  52. /**
  53. * Bootstrap the test.
  54. *
  55. * @param args Command line arguments are not currently used.
  56. */
  57. public static void main(String[] args) {
  58. new HadoopTestApp().run(args);
  59. }
  60. /**
  61. * If you prefer to trace variables manually. Or you can set some breakpoints
  62. * and inspect variables in the debugger.
  63. */
  64. public void doTrace(String label, Object o) {
  65. System.out.print(label + ": ");
  66. System.out.println(o);
  67. }
  68. /**
  69. * Run the test of Hooch's algorithm.
  70. *
  71. * @param args Command line arguments are not currently used.
  72. */
  73. public void run(String[] args) {
  74. doTrace("treemap", treemap); // NEW
  75. ArrayList<Integer> currentArray;
  76. ArrayList<Integer> compareArray;
  77. // Setting user friend list
  78. // cycle users "i"
  79. for (int i = 1; i <= treemap.size(); i++) {
  80. currentArray = treemap.get(i);
  81. // cycle user "k"
  82. for (int k = 1; k <= treemap.size(); k++) {
  83. // if(i!=k){ Put back in once working
  84. compareArray = treemap.get(k);
  85. // cylce "k"s movie list
  86. for (int l = 0; l < compareArray.size(); l++) {
  87. if (currentArray.contains(compareArray.get(l))) {
  88. // NEW I set a debugger breakpoint on this line to inspect all variables
  89. if (treemap2.containsKey(i)) {
  90. ArrayList<Integer> list3 = treemap2.get(i);
  91. list3.add(k);
  92. treemap2.remove(i);
  93. treemap2.put(i, list3);
  94. doTrace("contains key, treemap2", treemap2); // NEW
  95. }
  96. if (!treemap2.containsKey(i)) {
  97. ArrayList<Integer> list4 = new ArrayList<Integer>();
  98. list4.add(k);
  99. treemap2.put(i, list4);
  100. doTrace("does not contain key, treemap2", treemap2); // NEW
  101. }
  102. }
  103. }
  104. }
  105. }
  106. // Create string of friends
  107. for (ArrayList<Integer> x : treemap2.values()) {
  108. str2 = Integer.toString(x.get(0));
  109. for (int i = 1; i < x.size(); i++) {
  110. str2 = str2 + "," + Integer.toString(x.get(i));
  111. }
  112. doTrace("in loop str2", str2); // NEW
  113. }
  114. // context.write(key, new Text(str2));
  115. doTrace("context.write str2", str2); // NEW
  116. }
  117. }
展开查看全部

相关问题