我要做的是从一个包含500,000个英文单词的文本文件中读取并将其存储到HashSet中以达到性能目的。然后我想返回一个包含限定元素的HashSet,例如,5个字母的单词,6个字母的单词等。
这是我的代码到目前为止,我不知道如何做到这一点。 我感谢您的解决方案!
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);
}
}
答案 0 :(得分:-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.........
}