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()创建一个hashmap,其中包含两个字符串中使用的单词,并记住单词和单词使用的次数。 printArray(HashMap printHashMap)用于遍历在getWordCounts()中创建的HashMap并打印出数据。
答案 0 :(得分:2)
将getWordCounts
方法更改为:
protected Map<String, Integer> getWordCounts()
{
String[] wordArray = getWordArray();
Map<String, Integer> wordCounts = new HashMap<String, Integer>();
List<String> list = Arrays.asList(wordArray);
Set<String> set = new HashSet<String>(list);
for (String str : set) {
wordCounts.put(str, Collections.frequency(list, str));
}
return wordCounts;
}
要打印地图元素,请将printArray
方法更改为:
protected void printArray(HashMap<String, Integer> printHashMap){
Iterator<Entry<String, Integer>> iter = printHashMap.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Integer> next = iter.next();
Integer value = next.getValue();
System.out.println("key = " + next.getKey());
System.out.println("value = " + value);
}
}
答案 1 :(得分:1)
我已将代码更改为:
import java.util.*;
import java.lang.Iterable;
public class Main {
String words;
public static void main(String[] args) {
Main plato = new Main(
"You can discover more about a person in an hour of play than in a year of conversation");
Main roosevelt = new Main(
"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);
System.out.println("____________________________________________________");
roosevelt.printArray(roosevelt.getWordCounts());
System.out.println("____________________________________________________");
}
public Main(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);
}
}
/*Changed*/
protected void printArray(HashMap<String, Integer> printHashMap)
{
for(String string: printHashMap.keySet())
{
System.out.println(string + ":" + printHashMap.get(string) );
}
}
/*changed*/
protected HashMap<String, Integer> getWordCounts()
{
String[] wordArray = getWordArray();
HashMap<String, Integer> wordCounts = new HashMap<String, Integer>();
for(String string: wordArray)
{
if(wordCounts.containsKey(string)){
wordCounts.put(string, wordCounts.get(string) + 1);
}else{
wordCounts.put(string, 1);
}
}
return wordCounts;
}
}