2017-02-07 38 views
-1

我想要做的是從包含500,000英文單詞的文本文件中讀取並將其存儲到HashSet以達到性能目的。然後我想返回一個包含合格元素的HashSet,例如5個字母的單詞,6個字母的單詞等。如何遍歷HashSet並返回帶有限定元素的集合?

這是我的代碼,至今我不知道該怎麼做。 我很感謝你的解決方案!

private static HashSet<String> readPuzzleFile(int wordLength) { 
     HashSet<String> unProcessedPuzzle = new HashSet<String>(); 
     try (Scanner file = new Scanner(new File(PATHTOPUZZLE))) { 
      while (file.hasNextLine()) { 
       unProcessedPuzzle.add(file.nextLine()); 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println("No Puzzle File Found!"); 
      return null; 
     } 
     HashSet<String> puzzle = new HashSet<String>(); 
     Iterator iterator = unProcessedPuzzle.iterator(); 
     for (String word : unProcessedPuzzle){ 
      puzzle.addAll(word); 
     } 
     } 
+1

而且你有什麼問題或你的問題? – IQV

+0

@IQV如何做到這一點,抱歉忘了第一次把它放在問題中。 –

+0

如何做什麼? – shmosel

回答

-1
private static HashSet<String> readPuzzleFile(int wordLength) { 
     HashSet<String> unProcessedPuzzle = new HashSet<String>(); 
     try (Scanner file = new Scanner(new File(PATHTOPUZZLE))) { 
      while (file.hasNextLine()) { 
       unProcessedPuzzle.add(file.nextLine()); 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println("No Puzzle File Found!"); 
      return null; 
     } 
     HashSet<String> puzzle = new HashSet<String>(); 
     Iterator iterator = unProcessedPuzzle.iterator(); 
     for (String word : unProcessedPuzzle){ 
      puzzle.add(word); //........addAll -> add......... 
     } 
     return puzzle;//........return puzzle......... 
     } 
+0

雖然這不是正確的方法,但是你激勵了我。現在我解決了,謝謝! –