将字符串转换为HashMap / MultiSet?

时间:2013-06-28 19:10:23

标签: java split hashmap multiset

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

我会.split(“”)拆分空格并且可能是一个arraylist,然后可能会通过arraylist并将每个项目添加到hashmap或multiset?我不确定这样做的最佳方法是/如果可以直接使用某种类型的hashmap完成而不先制作一个arraylist。

谢谢!

2 个答案:

答案 0 :(得分:3)

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

HashMultiset.create(
  Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings()
    .split(string));

答案 1 :(得分: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));
        }
    }
}