0

我手头有很多话。我需要做的是保存它们并计算每个不同的单词。原始数据可能包含一些重复的单词。首先,我想使用 Set,然后我可以保证我只得到不同的 wrods。但是我怎么能数他们的时间呢?有没有人有任何“聪明”的想法?

4

3 回答 3

3

您可以MultiSet从 Guava 库中使用。

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multiset.html

于 2013-03-14T02:25:05.867 回答
2

您可以使用 Map 来解决此问题。

String sample = " I have a problem here. I have a lot of words at hand. What I need to do is to save them and count every different word. The original data may contains duplicate words.Firstly, I want to use Set, then I can guarantee that I only get the different wrods. But how can I count their times? Is there someone having any clever idea?";
    String[] array = sample.split("[\\s\\.,\\?]");
    Map<String,Integer> statistic = new HashMap<String,Integer>();
    for (String elem:array){
        String trimElem = elem.trim();
        Integer count = 0;
        if(!"".equals(trimElem)){
            if(statistic.containsKey(trimElem)){
                count = statistic.get(trimElem);
            }
            count++;
            statistic.put(trimElem,count);
        }
    }
于 2013-03-14T02:40:51.167 回答
1

也许你可以使用散列,在java中,它是HashMap(或HashSet?)你可以散列每个单词,如果该单词已被散列,则将与之相关的一些值加一,这就是想法。

于 2013-03-14T02:57:12.430 回答