我的代码是:
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);
}
}
}
词组类:
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;
}
}
我想要做的是使用 getWordSet() 方法,使用两个 WordGroups 并迭代或循环返回的 HashSet 并从中打印单词。
在两个 WordGroup 上调用 getWordCounts()。使用 keySet() 检索键集。循环这个集合并打印出两个 WordGroups 的单词及其计数。
使用 getWordSet() 方法对两个 WordGroups 中的所有单词进行完整的集合。循环遍历新的 HashSet 以打印所有单词的完整列表,其中包含每个 hashmap 的总计数。
我正在努力应对所有这些。任何帮助深表感谢!!