Putting words from an array into a HashMap and a HashSet

与世无争的帅哥 提交于 2019-12-24 19:27:36

问题


I am coding in blueJ and what I am trying to do is this:

1.a) Create a getWordSet() method in WordGroup which:

  • takes another WordGroup object as a parameter
  • creates a HashSet<String>
  • uses two for loops to put all the words from this and the parameter WordGroup into the HashSet
  • returns the HashSet<String>

1.b) In the main method:

  • use the getWordSet() method using the two WordGroups
  • iterate or loop over the HashSet returned and print the words from it

2.a) Create a method in the WordGroup called getWordCounts() which:

  • creates a HashMap<String, Integer>
  • loops over all the words returned by getWordArray() and puts each word into the HashMap with the number of times it occurs
  • returns HashMap<String, Integer>

2.b) In the main method:

  • call getWordCounts() on the two WordGroups
  • use keySet() to retrieve the set of keys (the String part of the mapping)
  • loop over this set and print out the word and its count for both WordGroups
  • use the getWordSet() method to make complete set of all the words from both WordGroups
  • loop over the new HashSet to print a complete list of all words with the sum counts from each of the HashMaps

My code so far:

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

WordGroup class:

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>();
        for (String words : quoteOne){
            words.add(word);
        }
        return words;
    }

    public HashMap<String, Integer> getWordCounts() {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (String words : words) {
            words.add(word);
        }
        return HashMap<String, Integer>;
    }

}    

I have got this far and now I am stuck. I cannot figure out how to get the words from teh array into the hashset and hashmap and how to return them in the desired form. p.s. sorry about the wierd question layout- the string kept disappearing after hashset if it was not in the code format)


回答1:


There are some basic mistakes here that you'll need to sort out before you can go any further.

Let's look at this method first.

public HashSet<String> getWordSet(){
    HashSet<String> set = new HashSet<String>();
    for (String words : quoteOne){
        words.add(word);
    }
    return words;
}

Now, you haven't set quoteOne to anything yet, at least not in this class. And what you want it to be is the return value from calling getWordArray(). So it would be good to include the line

String[] quoteOne = getWordArray();

somewhere in this method.

Next, you're trying to reuse a variable name. You've already got words in this class, so to avoid confusion, it would be better if you use a different name for the String variable that you iterate through the loop with.

Now, you're trying to add strings to words, when you actually want to add them to set, because set is the thing that you'll be returning from this method. So as well as changing the line with add, you'll also want to change the line with return, to make everything match.

So think very carefully about what each variable is for, and make sure that you use each of them correctly. Some of these types of errors will be caught by the compiler, but that's not an excuse for being sloppy with your use of variables.

There are actually shorter ways of turning an array into a HashSet, but I think it would be a good exercise if you try to get it correct, using this way of doing it first.

Now let's look at the next method.

public HashMap<String, Integer> getWordCounts() {
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    for (String words : words) {
        words.add(word);
    }
    return HashMap<String, Integer>;
}

You've got the right basic idea, but you're missing a couple of key steps. Firstly, just like in the previous method, you'll need a call to getWordArray(). But now, you need one more step.

As you iterate through the loop, you'll need to look in the HashMap, using the get method, to see if a particular word has already been recorded there. If so, you'll need to see what Integer has been recorded against it, add one, then put it back in the map. If not, you can just put something into the map without doing any arithmetic.

Also, please be aware that the method for adding things to any sort of map isn't add, it's put, and it needs two arguments - the key and the value. So at some point in your logic, you might have a line like

map.put(word, 1);

You can see the key and the value in this call.

Lastly, think about your return statement. You want to return a variable, not its type. Can you guess which variable you'll use there?

Good luck with finishing your assignment. I've tried to point you in the right directions without doing too much of this for you.



来源:https://stackoverflow.com/questions/19896467/putting-words-from-an-array-into-a-hashmap-and-a-hashset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!