Java 遍历哈希集并打印结果

Java 遍历哈希集并打印结果,java,printing,hashset,Java,Printing,Hashset,我试图在我的主类的2个字组上使用我的字组类中的getWordSet()方法,并循环返回的哈希集并打印出结果,但它甚至没有编译。我的代码尝试,我希望能做到这一点,是在我的主要类的底部之间的星号。谁能帮帮我吗?编辑:我已经实现了如下代码所示的建议更改,这导致了一个新问题-我希望两个字组进入同一个集合,但当我尝试将hashSTwo更改为hashSOne,试图将所有字都转换为hashSOne时,它无法编译 import java.util.HashSet; import java.util.HashMa

我试图在我的主类的2个字组上使用我的字组类中的getWordSet()方法,并循环返回的哈希集并打印出结果,但它甚至没有编译。我的代码尝试,我希望能做到这一点,是在我的主要类的底部之间的星号。谁能帮帮我吗?编辑:我已经实现了如下代码所示的建议更改,这导致了一个新问题-我希望两个字组进入同一个集合,但当我尝试将hashSTwo更改为hashSOne,试图将所有字都转换为hashSOne时,它无法编译

import java.util.HashSet;
import java.util.HashMap;

public class Main{

    public static void main(String[] args){
        WordGroup wordgroupOne= new WordGroup ("You can discover more about a person in an hour of play than in a year of conversation");
        WordGroup wordgroupTwo= new WordGroup ( "When you play play hard when you work dont play at all");

        String[] quoteOne = wordgroupOne.getWordArray();   
        String[] quoteTwo = wordgroupTwo.getWordArray();

        for (String words : quoteOne){
            System.out.println(words);
        }

        for (String words : quoteTwo){                       
            System.out.println(words);
        }

        **HashSet<String> hashSOne = wordgroupOne.getWordSet();
        HashSet<String> hashSTwo = wordgroupTwo.getWordSet();

        for (String set : hashSOne){
            System.out.println(set);
        }

        for (String set : hashSTwo){
            System.out.println(set);
        }**
    }
}
import java.util.HashSet;
导入java.util.HashMap;
公共班机{
公共静态void main(字符串[]args){
WordGroup wordgroupOne=新的WordGroup(“你可以在一小时的游戏中比一年的对话中发现更多关于一个人的信息”);
WordGroup wordgroupTwo=new WordGroup(“当你努力玩的时候,当你工作的时候,根本不玩”);
字符串[]quoteOne=wordgroupOne.getWordArray();
字符串[]quoteTwo=wordgroupTwo.getWordArray();
for(字符串:quoteOne){
System.out.println(字);
}
对于(字符串字:quoteTwo){
System.out.println(字);
}
**HashSet hashSOne=wordgroupOne.getWordSet();
HashSet hashSTwo=wordgroupTwo.getWordSet();
for(字符串集:hashSOne){
系统输出打印项次(套);
}
for(字符串集:hashSTwo){
系统输出打印项次(套);
}**
}
}
字组集:

import java.util.HashSet;
import java.util.HashMap;

public class WordGroup {  
    public String words;

    public WordGroup (String getWords){
        words = getWords.toLowerCase();
    }

    public String[] getWordArray(){
        return words.split(" ");   
    }

    public HashSet<String> getWordSet(){
        HashSet<String> set = new HashSet<String>();
        String[] p = getWordArray();
        for (String items : p){
            set.add(items);
        }
        System.out.println(set);
        return set;
    }

    public HashMap<String, Integer> getWordCounts() {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        String[] q = getWordArray();
        for (String stuff : q) {
            Integer oldVal = map.get(stuff);
            if (oldVal == null){
                oldVal = 0;
            }
            map.put(stuff, oldVal+1);
        }
        System.out.println(map);
        return map;
    }
}
import java.util.HashSet;
导入java.util.HashMap;
公共类字组{
公共字符串;
公共字组(字符串getWords){
words=getWords.toLowerCase();
}
公共字符串[]getWordArray(){
返回单词。拆分(“”);
}
公共HashSet getWordSet(){
HashSet=newhashset();
字符串[]p=getWordArray();
对于(字符串项:p){
设置。添加(项目);
}
系统输出打印项次(套);
返回集;
}
公共HashMap getWordCounts(){
HashMap=newHashMap();
字符串[]q=getWordArray();
对于(字符串内容:q){
整数oldVal=map.get(stuff);
如果(oldVal==null){
oldVal=0;
}
map.put(stuff,oldVal+1);
}
系统输出打印项次(map);
返回图;
}
}

可以从查看getWordArray()方法开始。我怀疑你的正则表达式有点闪避。
有关空格拆分正则表达式,请参见此链接。

您不应该在
quoteOne
上使用
getWordSet()
,因为它是
String[]
,并且没有这种方法。您可能想在
wordgroupOne
上使用它,这是
WordGroup
类型。这同样适用于
quotewo

如果可能的话,您应该更喜欢而不是实际的类,因此可以将方法更改为返回
Set
,而不是
HashSet


如何将两个结果放在同一个哈希集中?我试着把它们都改成hashSOne,它只是用第二个hashset覆盖了我的第一个hashset

你可能做过

hashSOne = hashSTwo;
这只会使
hashSOne
reference使用
hashSTwo
reference中的集合

如果您想创建一个包含两个集合中所有项目的新集合,可以这样做

//lets create Set with elements from first set
Set<String> allElements = new HashSet<>(hashSOne);

//now lets add all elements from second set 
allElements.addAll(hashSTwo);//this will not add duplicates
//让我们使用第一个集合中的元素创建集合
Set等位基因=新HashSet(hashSOne);
//现在让我们添加第二个集合中的所有元素
等位基因。addAll(hashSTwo)//这不会添加重复项

对于以后的文章,你应该更明确地说明错误是什么。如果没有编译器的帮助,可能很难找到错误

HashSet<String> hashSOne = quoteOne.getWordSet();
HashSet<String> hashSTwo = quoteTwo.getWordSet();
HashSet hashSOne=quoteOne.getWordSet();
HashSet hashSTwo=quoteTwo.getWordSet();
应该是

HashSet<String> hashSOne = wordgroupOne.getWordSet();
HashSet<String> hashSTwo = wordgroupTwo.getWordSet();
HashSet hashSOne=wordgroupOne.getWordSet();
HashSet hashSTwo=wordgroupTwo.getWordSet();

但它不起作用
不是解释问题的最佳方法。也许可以添加更多关于它应该如何工作以及它现在的行为的细节。你说的“它不工作”是什么意思?什么不工作?它不工作没有帮助。到底发生了什么?有错误信息吗?如果是,请将其粘贴到问题中。告诉我们你期望会发生什么,以及会发生什么。谢谢你,我已经做到了,而且效果很好——但不是我希望的那样。如何将两个结果放在同一个哈希集中?我试着把它们都改成hashSOne,但没有编译。