将数组中的单词放入HashMap和HashSet中 [英] Putting words from an array into a HashMap and a HashSet

查看:177
本文介绍了将数组中的单词放入HashMap和HashSet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在blueJ中编写代码,我试图做的是这样的:

1.a)创建一个<$ c $其中:


    中的c> getWordSet()> WordGroup li>以另一个> WordGroup 对象作为参数
  • 创建 a HashSet< String>
  • 使用两个for循环将来自this的所有单词和参数 WordGroup 放入 HashSet

  • 返回 HashSet< String>



1.b)在主要方法中:


  • 使用 WordGroup s 来使用 getWordSet() li>
  • HashSet 进行迭代或循环,并从中打印出单词


2.a) WordGroup 中创建一个名为 getWordCounts() code>其中:


  • 创建一个 HashM ap< String,Integer>

  • 遍历所有由 getWordArray()返回的单词,在$ HashMap 中加入它的次数

  • 返回 HashMap< String,Integer>
    $ b 2.b)主要
  • code>方法:


    • 调用 getWordCounts() code> WordGroup s

    • 使用 keySet()来检索一组键值映射的字符串部分)

    • 循环遍历此集合,并为 WordGroup s
    • 输出单词及其计数。
    • 使用 getWordSet()方法从 WordGroup s

    • 循环遍历新的 HashSet 以打印所有单词的完整列表,其中包含每个 HashMap s



    到目前为止我的代码:

      public class main {

    public static void main(String [] args){
    WordGroup wordgroupOne = new WordGroup(您可以在一小时内发现一个人的更多信息,而不是年的谈话);
    WordGroup wordgroupTwo =新的WordGroup(当你在工作时不玩耍时玩耍很难);

    String [] quoteOne = wordgroupOne.getWordArray();
    String [] quoteTwo = wordgroupTwo.getWordArray();

    for(String words:quoteOne){
    System.out.println(words);


    for(String words:quoteTwo){
    System.out.println(words);


    $ b

    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);
    }
    返回单词;
    }

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

    }

    我被卡住了。我无法弄清楚如何从数组中获取单词到哈希集和散列表以及如何以期望的形式返回它们。附:对于奇怪的问题布局感到抱歉 - 如果字符串不是代码格式,字符串在哈希集合之后会一直消失)

有一些基本的错误,你需要先解决,然后才能继续。



让我们先看看这个方法。

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

$ / code>

现在,您还没有设置 quoteOne 到任何东西,至少不是在这个类中。而你想要的是调用 getWordArray()的返回值。所以最好包含这行

  String [] quoteOne = getWordArray(); 

在这个方法中的某处。



下一步,你正试图重用一个变量名称。你已经在这个类中得到了 words ,所以为了避免混淆,如果你为 String 你通过循环迭代的变量。



现在,你正试图将字符串添加到 words set 时,因为 set 是你要返回的东西从这个方法。因此,除了使用 add 更改行外,您还需要将行更改为 return ,以使一切都相配。

因此,仔细考虑每个变量的用途,并确保正确使用每个变量。这些类型的错误中的一些会被编译器捕获,但这并不是你使用变量时sl ex的借口。



实际上有更短的转换方法数组放入 HashSet 中,但我认为如果您尝试正确地使用这种方法,这将是一个很好的练习。



现在我们来看下一个方法。

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

你有正确的基本想法,但你错过了一对夫妇关键步骤。首先,就像在前面的方法中一样,你需要调用 getWordArray()。但现在,你需要多一步。

在遍历循环时,您需要查看 HashMap ,使用 get 方法,以查看某个特定单词是否已经在那里记录。如果是这样,你需要看看> Integer 是否已经被记录,添加一个,然后放回地图。如果不是的话,你可以在没有任何算术的情况下将一些东西放到地图中。



另外,请注意,将东西添加到任何地图的方法不是 add ,它是 put ,它需要两个参数 - 键和值。因此,在你的逻辑中的某个时刻,你可能会有一行像

  map.put(word,1); 

您可以在此调用中看到关键字和值。



最后,考虑你的 return 语句。你想返回一个变量,而不是它的类型。你能猜到你将使用哪个变量吗?



祝你完成作业。我试图指出你正确的方向,而不会为你做太多这样的事。


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)

解决方案

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.

这篇关于将数组中的单词放入HashMap和HashSet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆