【问题标题】:Fast way of counting number of occurrences of a word in a string using Java使用Java计算字符串中单词出现次数的快速方法
【发布时间】:2017-02-01 04:31:40
【问题描述】:

我想使用 Java 快速有效地查找一个单词在字符串中出现的次数。

单词以空格分隔,我正在寻找完整的单词。

Example: 
string: "the colored port should be black or white or brown"
word: "or"
output: 2

对于上面的例子,“colored”和“port”不计算在内,但是“or”被计算在内。

我考虑过使用 substring()contains() 并迭代字符串。但是接下来我们需要检查我认为效率不高的周围空间。 StringUtils.countMatches() 效率也不高。

我尝试过的最好的方法是在空间上分割字符串并迭代单词,然后将它们与给定的 word 匹配:

String string = "the colored port should be black or white or brown";
String[] words = string.split(" ");
String word = "or";
int occurrences = 0;
for (int i=0; i<words.length; i++)
    if (words[i].equals(word))
        occurrences++;
System.out.println(occurrences);

但我期待一些使用 Matcherregex 的有效方法。

于是我测试了以下代码:

        String string1 = "the colored port should be black or white or brown or";
        //String string2 = "the color port should be black or white or brown or";
        String word = "or";
        Pattern pattern = Pattern.compile("\\s(" + word + ")|\\s(" + word + ")|(" + word + ")\\s");
        Matcher  matcher = pattern.matcher(string1);
        //Matcher  matcher = pattern.matcher(string2);
        int count = 0;
        while (matcher.find()){
            match=matcher.group();
            count++;
        }
        System.out.println("The word \"" + word + "\" is mentioned " + count + " times.");

它应该足够快,并为我提供了 string1 的正确答案,但不是 string2(已评论)。正则表达式似乎需要稍作改动。

有什么想法吗?

【问题讨论】:

  • 当您搜索“java 快速字符串匹配”或“java 快速字数统计”时,您发现了什么?
  • int output = Collections.frequency(Arrays.asList(string.split(" ")), word);
  • "the colored port should be black or white or brown".split(" or ").length-1);?
  • 你可以使用Baby的解决方案,在开头和结尾添加一个空格
  • 所以您不是从文件中读取文本?

标签: java regex find-occurrences


【解决方案1】:

我对三个答案进行了实验和评估; split basedMatcher based(如问题中所述)和 Collections.frequency() 基于(如@上面的评论中所述4城堡)。每次我测量一个循环重复 1000 万次的总时间。因此,基于拆分的答案往往是最有效的方式

String string = "the colored port should be black or white or brown";
String[] words = string.split(" ");
String word = "or";
int occurrences = 0;
for (int i=0; i<words.length; i++)
    if (words[i].equals(word))
        occurrences++;
System.out.println(occurrences);

然后是基于 Collections.frequency() 的答案,运行时间稍长(慢约 5%):

String string = "the colored port should be black or white or brown or";
String word = "or";
int count = Collections.frequency(Arrays.asList(string.split(" ")), word);
System.out.println("The word \"" + word + "\" is mentioned " + count + " times.");

基于匹配器的解决方案(在问题中提到)要慢得多(运行时间增加约 5 倍)。

【讨论】:

    【解决方案2】:
    public class Test {
    public static void main(String[] args) {
        String str= "the colored port should be black or white or brown";
        Pattern pattern = Pattern.compile(" or ");
        Matcher  matcher = pattern.matcher(str);
    
        int count = 0;
        while (matcher.find())
            count++;
    
        System.out.println(count);    
    }
    

    }

    【讨论】:

      【解决方案3】:

      这个怎么样?假设word 不会有空格。

      string.split("\\s"+word+"\\s").length - 1;
      

      【讨论】:

      • @NickZiebert,发布您的单独查询,截至目前,OP 只需要一个词即可搜索
      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2021-01-09
      相关资源
      最近更新 更多