简体   繁体   English

将单词从数组放入HashMap和HashSet

[英]Putting words from an array into a HashMap and a HashSet

I am coding in blueJ and what I am trying to do is this: 我正在blueJ中进行编码,而我想这样做的是:

1.a) Create a getWordSet() method in WordGroup which: 1.A)创建getWordSet()在方法WordGroup其中:

  • takes another WordGroup object as a parameter 将另一个WordGroup对象作为参数
  • creates a HashSet<String> 创建a HashSet<String>
  • uses two for loops to put all the words from this and the parameter WordGroup into the HashSet 使用两个for循环WordGroup所有单词和参数WordGroup放入HashSet
  • returns the HashSet<String> 返回HashSet<String>

1.b) In the main method: 1.b)main方法中:

  • use the getWordSet() method using the two WordGroup s 通过两个WordGroup使用getWordSet()方法
  • iterate or loop over the HashSet returned and print the words from it 迭代或循环返回的HashSet并从中打印单词

2.a) Create a method in the WordGroup called getWordCounts() which: 2.a)WordGroup创建一个名为getWordCounts()的方法,该方法:

  • creates a HashMap<String, Integer> 创建一个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 getWordArray()返回的所有单词,并将每个单词及其出现的次数放入HashMap
  • returns HashMap<String, Integer> 返回HashMap<String, Integer>

2.b) In the main method: 2.b)main方法中:

  • call getWordCounts() on the two WordGroup s 在两个WordGroup上调用getWordCounts()
  • use keySet() to retrieve the set of keys (the String part of the mapping) 使用keySet()检索键集(映射的String部分)
  • loop over this set and print out the word and its count for both WordGroup s 遍历此集合并打印出两个WordGroup的单词及其计数
  • use the getWordSet() method to make complete set of all the words from both WordGroup s 使用getWordSet()方法对两个WordGroup的所有单词进行完整设置
  • loop over the new HashSet to print a complete list of all words with the sum counts from each of the HashMap s 循环遍历新的HashSet以打印所有单词的完整列表,以及每个HashMap的总计数

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: WordGroup类别:

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. 我无法弄清楚如何将单词从数组转换为哈希集和哈希图,以及如何以所需的形式返回它们。 ps sorry about the wierd question layout- the string kept disappearing after hashset if it was not in the code format) ps对这个奇怪的问题布局感到抱歉-如果字符串不是代码格式,则字符串在哈希集后会不断消失)

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. 现在,您还没有将quoteOne设置为任何值,至少在此类中尚未设置。 And what you want it to be is the return value from calling getWordArray() . 您想要的是调用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. 此类中已经有words ,因此为避免混淆,如果为循环遍历的String变量使用其他名称,则更好。

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. 现在,当您实际上想将字符串添加到set时,您尝试将字符串添加到words ,因为set是您要从此方法返回的东西。 So as well as changing the line with add , you'll also want to change the line with return , to make everything match. 因此,除了使用add更改行之外,还需要使用return更改行以使所有内容匹配。

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. 实际上,有几种将数组转换为HashSet较短方法,但是我认为如果您首先尝试使用此方法来使其正确,那将是一个很好的练习。

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() . 首先,就像前面的方法一样,您需要调用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. 遍历循环时,需要使用get方法查看HashMap ,以查看是否已经在其中记录了特定单词。 If so, you'll need to see what Integer has been recorded against it, add one, then put it back in the map. 如果是这样,则需要查看针对它记录了什么Integer ,添加一个,然后将其放回地图中。 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. 另外,请注意,将事物添加到任何类型的地图的方法不是add ,而是put ,它需要两个参数-键和值。 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. 最后,考虑您的return声明。 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. 我试图为您指明正确的方向,但又不会为您做太多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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