0

将长字符串转换为包含单词和计数的数据结构的最佳方法是什么。

我会做 .split(" ") 在空格上拆分并大概制作一个数组列表,然后可能会通过数组列表并将每个项目添加到哈希图或多重集?我不确定最好的方法是什么/是否可以直接使用某种哈希图来完成,而无需先创建数组列表。

谢谢!

4

2 回答 2

3

如果您指的是Guava Multiset,这只是一行

HashMultiset.create(
  Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings()
    .split(string));
于 2013-06-28T19:22:48.980 回答
1
import java.util.HashMap;
import java.util.Map;

public class Test {
    private static Map<String, Integer> count = new HashMap<String, Integer>();

    public static void main(String[] args) {
        addToCountMap("This is my test string and it contains Test and test and string and some more");
        addToCountMap("This is my test string and it contains Test and test and string and some more");
        addToCountMap("This is my test string and it contains Test and test and string and some more");
        addToCountMap("This is my test string and it contains Test and test and string and some more");
        addToCountMap("This is my test string and it contains Test and test and string and some more");

        mergeWithCountMap(count);

        System.out.println(count);
    }

    private static void addToCountMap(String test) {
        String[] split = test.split(" ");
        for (String string : split) {
            if (!count.containsKey(string)) {
                count.put(string, 0);
            }
            count.put(string, count.get(string) + 1);
        }
    }

    private static void mergeWithCountMap(Map<String, Integer> mapToMerge) {
        for (String string : mapToMerge.keySet()) {
            if (!count.containsKey(string)) {
                count.put(string, 0);
            }
            count.put(string, count.get(string) + mapToMerge.get(string));
        }
    }
}
于 2013-06-28T19:17:22.380 回答