2016-03-03 89 views
0

我想在Hadoop中的一個名爲outputList的ArrayList中刪除重複的字符串。Java ArrayList <String> .contains()hadoop

這裏是我的代碼:

List<String> newList = new ArrayList<String>(); 

    for(String item : outputList){ 
     if(!newList.contains(item)) 
     newList.add(item); 
     else newList.add("wrong"); 
    } 

的問題是,在newList字符串都是「錯誤的」。

一些事實: 1.上面的代碼在本地機器上運行良好。

  1. 我可以在hadoop的outputList中寫出字符串。 outputList中的大多數字符串都不相同(存在重複)。

  2. 我嘗試了一些其他的方法來刪除重複的項目。就像使用HashSet一樣。但是當我使用outputList來初始化一個HashSet時,獲得的HashSet是空的。

  3. 在Hadoop中的Java版本是javac的1.6.0_18

感謝。

以下是我的減速器代碼:

public static class EditReducer 
     extends Reducer<Text,Text,Text,Text> { 

    private Text editor2 = new Text(); 

    public void reduce(Text key, Iterable<Text> values, 
         Context context 
         ) throws IOException, InterruptedException { 
     //write the content of iterable to an array list. 

    List<String> editorList =new ArrayList<String>(); 
    for (Text t:values) { 
     editorList.add(t.toString()); 

    } 


    //if a user appears more than once in the list, add to outputList 
    int occ; 
    List<String> outputList =new ArrayList<String>(); 

    for (int i=0;i<editorList.size();i++) { 

     occ= Collections.frequency(editorList, editorList.get(i)); 
     if(occ>1) { 
     outputList.add(editorList.get(i)); 
     } 
    } 



    //make outputList distinct 
    List<String> newList = new ArrayList<String>(); 

    for(String item : outputList){ 
     if(!newList.contains(item)) 
     newList.add(item); 
     else newList.add("wrong"); 
    } 

     for (String val : newList) { 
     editor2.set(val); 
     context.write(editor2,editor2); 
     } 
    } 

    } 
+0

@讓FrançoisSavard我有讀取列表中的輸入以計算頻率。 – Paradox

+0

我爲我的減速器添加了代碼以使其清晰@ Jean-FrançoisSavard – Paradox

+0

什麼是editor2?寫(editor2,editor2)方法是做什麼的? –

回答

-1

您可以製作自己的原創for循環內嵌套for循環和比較字符串方式:

List<String> newList = new ArrayList<String>(); 

    for(String item : outputList) { 
     boolean contains = false; 
     for(String str: newList) { 
      if(str.equals(item)) { 
       contains = true; 
       break; 
      } 
     } 
     if(!contains) { 
      newList.add(item); 
     } 
     else { 
      newList.add("wrong"); 
     } 
    } 
+0

newList中的項目仍然是「錯誤的」 – Paradox

+0

實際上不需要重新發明輪子,你的內部循環做同樣的事情就像'contains'方法 – user902383

+0

我添加了我的reducer在代碼中的代碼 – Paradox

相關問題