import java.util.*;
import java.lang.Iterable;
public class WordGroup {
String words;
public static void main(String[] args) {
WordGroup plato = new WordGroup(
"You can discover more about a person in an hour of play than in a year of conversation");
WordGroup roosevelt = new WordGroup(
"When you play play hard when you work dont play at all");
String[] platoArray = plato.getWordArray();
String[] rooseveltArray = roosevelt.getWordArray();
plato.printArray(platoArray);
roosevelt.printArray(rooseveltArray);
System.out.println("____________________________________________________");
HashSet<String> rooseveltWordSet = roosevelt.getWordSet(platoArray);
roosevelt.printArray(rooseveltWordSet);
}
public WordGroup(String word) {
words = word.toLowerCase();
}
protected String[] getWordArray() {
String[] wordArray = words.split(" ");
return wordArray;
}
protected HashSet<String> getWordSet(String[] groupedWords)
{
HashSet<String> wordSet = new HashSet<String>();
for (String string : groupedWords) {
wordSet.add(string);
}
String[] wordArray = getWordArray();
for (String string : wordArray) {
wordSet.add(string);
}
return wordSet;
}
protected void printArray(String[] array) {
for (String string : array) {
System.out.println(string);
}
}
protected void printArray(HashSet<String> hashWords)
{
for(String hashset: hashWords)
{
System.out.println(hashset);
}
}
protected void printArray(HashMap<String, Integer> printHashMap)
{
for(String string: printHashMap)
{
System.out.println(string);
}
}
protected HashMap<String, Integer> getWordCounts()
{
String[] wordArray = getWordArray();
HashMap<String, Integer> wordCounts = new HashMap<String, Integer>();
for(String string: wordArray)
{
int one = 1;
wordCounts.put(string, +one);
}
return null;
}
}
我试图让 getWordCounts() 制作一个哈希图,其中包含两个字符串中使用的单词并记住该单词和该单词的使用次数。printArray(HashMap printHashMap) 用于迭代在 getWordCounts() 中创建的 HashMap 并打印出数据。