2017-02-07 37 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

虽然这不是正确的方法,但是你激励了我。现在我解决了,谢谢! –