简体   繁体   中英

Creating and printing HashMap<String, Integer>

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;

    }
}

Im trying to make getWordCounts() to make a hashmap that contains the words used in two strings and remembering the word and the amount of times that word is used. printArray(HashMap printHashMap) is ment to Iterate through the HashMap created in getWordCounts() and print out the data.

Change your getWordCounts method to this:

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;
}

and to print the elements of the map, change printArray method to this:

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);
    }
}

I've changed your code to work:

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;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM