Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何遍历哈希集并返回包含限定元素的集?_Java_Loops_Hash_Iterator_Hashset - Fatal编程技术网

Java 如何遍历哈希集并返回包含限定元素的集?

Java 如何遍历哈希集并返回包含限定元素的集?,java,loops,hash,iterator,hashset,Java,Loops,Hash,Iterator,Hashset,我试图做的是从一个包含500000个英语单词的文本文件中读取数据,并将其存储到一个HashSet中以提高性能。然后我想返回一个包含限定元素的哈希集,例如,5个字母的单词,6个字母的单词,等等 到目前为止,这是我的代码,我不知道怎么做。 我感谢你的解决方案 private static HashSet<String> readPuzzleFile(int wordLength) { HashSet<String> unProcessedPuzzle = n

我试图做的是从一个包含500000个英语单词的文本文件中读取数据,并将其存储到一个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);
        }
       }
private静态HashSet readpuzzle文件(int-wordLength){
HashSet unProcessedPuzzle=新HashSet();
try(Scanner file=new Scanner(new file(PATHTOPUZZLE))){
while(file.hasNextLine()){
未处理的puzzle.add(file.nextLine());
}
}catch(filenotfounde异常){
System.out.println(“未找到拼图文件!”);
返回null;
}
HashSet puzzle=新HashSet();
迭代器迭代器=未处理的拼图。迭代器();
for(字符串字:未处理的拼图){
拼图。addAll(单词);
}
}
私有静态HashSet readpuzzle文件(int-wordLength){
HashSet unProcessedPuzzle=新HashSet();
try(Scanner file=new Scanner(new file(PATHTOPUZZLE))){
while(file.hasNextLine()){
未处理的puzzle.add(file.nextLine());
}
}catch(filenotfounde异常){
System.out.println(“未找到拼图文件!”);
返回null;
}
HashSet puzzle=新HashSet();
迭代器迭代器=未处理的拼图。迭代器();
for(字符串字:未处理的拼图){
拼图。添加(单词);/…添加所有->添加。。。。。。。。。
}
返回拼图;/……返回拼图。。。。。。。。。
}

您的问题是什么?@IQV如何解决,很抱歉第一次忘了把它放在问题中。怎么做?这听起来像是streams或Groovy迭代器方法的教科书案例。@TianchengXu如果你已经按照下面的评论解决了这个问题,那么请关闭问题或帖子并接受你的答案。虽然这不是正确的方法,但你启发了我。现在我解决了,谢谢!
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.........
       }