【问题标题】:Calling a method (hashmap/ hashset) from one class on a string in another.从一个类对另一个类的字符串调用方法(hashmap/hashset)。
【发布时间】:2013-11-23 23:47:51
【问题描述】:

我的代码是:

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

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>();
        String[] p = getWordArray();
        for (String items : p){
            set.add(items);
        }
        System.out.println(set);
        return set;
    }

    public HashMap<String, Integer> getWordCounts() {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        String[] q = getWordArray();
        for (String stuff : q) {
            Integer oldVal = map.get(stuff);
            if (oldVal == null){
                oldVal = 0;
            }
            map.put(stuff, oldVal+1);
        }
        System.out.println(map);
        return map;
    }

}

我想要做的是使用 getWordSet() 方法,使用两个 WordGroups 和 迭代或循环返回的 HashSet 并从中打印单词。

在两个 WordGroup 上调用 getWordCounts()。使用 keySet() 检索键集。循环这个集合并打印出两个 WordGroups 的单词及其计数。

使用 getWordSet() 方法对两个 WordGroups 中的所有单词进行完整的集合。 循环遍历新的 HashSet 以打印所有单词的完整列表,其中包含每个 hashmap 的总计数。

我正在努力应对所有这些问题。非常感谢任何帮助!

【问题讨论】:

    标签: java arrays hashmap hashset


    【解决方案1】:

    如果您想创建一个组合列表或集合,您必须将列表和地图合并在一起。我把那个练习留给你。

     public static void main(String[] args)
     {
    
        WordGroup wg1 = new WordGroup(
                "You can discover more about a person in an hour of play than in a year of conversation");
        WordGroup wg2 = new WordGroup(
                "When you play play hard when you work dont play at all");
    
        wg1.processWord();
    
        // iterate through all the distinct words
        Set<String> dw1 = wg1.getDistinctWords();
        for (String s : dw1)
        {
            System.out.println(s);
        }
    
        // use map entry to iterate through the entry set
        Map<String, Integer> wc1 = wg1.getWordCounts();
        for (Map.Entry<String, Integer> entry : wc1.entrySet())
        {
            if (entry != null)
            {
                // use stringbuilder to build a temp string
                // instead of using +
                StringBuilder sb = new StringBuilder();
                sb.append(entry.getKey());
                sb.append(": ");
                sb.append(entry.getValue());
    
                System.out.println(sb);
            }
        }
    
    }
    
     public class WordGroup
     {
    // as a class, made the results of the process private
    private String originalWord;
    // we declare generic versions of the Collections, instead of the specific
    // implementation
    private Set<String> distinctWords;
    private Map<String, Integer> wordCounts;
    
    public WordGroup(String s)
    {
        this.originalWord = s;
        // here we declare and initialize the specific implementation
        this.distinctWords = new HashSet<String>();
        this.wordCounts = new HashMap<String, Integer>();
    }
    
    public void processWord()
    {
        List<String> toProcess = getWordList();
        if (toProcess != null && !toProcess.isEmpty())
        {
            for (String s : toProcess)
            {
                // the set will automatically figure out if it should be in the
                // set or not.
                this.distinctWords.add(s);
                // call the update or insert method
                upsertString(s);
            }
        }
    }
    
    // this splits the string into a list
    // you could probably use a utility class from guava or something to do this
    // but i have coded a naive version
    private List<String> getWordList()
    {
        List<String> splitList = new ArrayList<String>();
    
        // check to see if there is anything there
        if (this.originalWord != null && !this.originalWord.isEmpty())
        {
            String lowered = this.originalWord.toLowerCase();
    
            String[] splits = lowered.split(" ");
            if (splits != null)
            {
                int iSize = splits.length;
                if (iSize > 0)
                {
                    // basically create a string
                    for (int i = 0; i < iSize; i++)
                    {
                        splitList.add(splits[i]);
                    }
                }
            }
        }
    
        return splitList;
    }
    
    // helper method to see if we need to add to the count
    private void upsertString(String s)
    {
        if (s != null && !s.isEmpty())
        {
            if (this.wordCounts != null)
            {
                // default to 1, if its an insert
                Integer newCount = 1;
    
                // if it already exists we want to update
                if (this.wordCounts.containsKey(s))
                {
                    Integer currentCount = this.wordCounts.get(s);
                    if (currentCount != null)
                    {
                        // update the count by 1
                        newCount += currentCount;
                    }
                }
    
                // insert the new item
                // or overwrite, because it is the same key to the new count
                this.wordCounts.put(s, newCount);
            }
        }
    }
    
    public String getOriginalWord()
    {
        return this.originalWord;
    }
    
    public void setOriginalWord(String originalWord)
    {
        this.originalWord = originalWord;
    }
    
    public Set<String> getDistinctWords()
    {
        return this.distinctWords;
    }
    
    public void setDistinctWords(Set<String> distinctWords)
    {
        this.distinctWords = distinctWords;
    }
    
    public Map<String, Integer> getWordCounts()
    {
        return this.wordCounts;
    }
    
    public void setWordCounts(Map<String, Integer> wordCounts)
    {
        this.wordCounts = wordCounts;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多