[英]Counting a certain character in a String (Java)
String a =“(Yeahhhh)我终于把它变成了(顶部)”;
给定上面的String,共有4个'('和')'。
我计算它的想法是利用String.charAt方法。 但是,这种方法相当慢,因为我必须为每个字符串执行此计数至少10000次,因为我的项目的性质。
任何人都有比使用.chartAt方法更好的想法或建议?????
对不起之前没有明确解释,我10000次的意思是分析10000个句子,这是上面的字符串a只作为一个句子。
听起来像是家庭作业,所以我会尽量保持“朝着正确的方向努力”。
如果您删除了所有字符而不是您要查找的字符,并查看该字符串的长度,该怎么办?
有一个String方法可以帮助你解决这个问题。
StringUtils.countMatches(wholeString, searchedString)
(来自commons-lang )
searchedString
可能是一个字符 - "("
它(如评论中所述charAt(..)
多次调用charAt(..)
。 但是,复杂性是多少? 好吧,它的O(n)
- charAt(..)
有复杂度O(1)
,所以我不明白你为什么觉得它很慢。
你可以使用toCharArray()
一次并迭代它。 它可能会更快。
为什么每个字符串需要10000次这样做? 你为什么不记得第一次的结果? 这比单个计数加速节省了很多。
您可以通过以下方法实现此目的。
此方法将返回一个带有键的映射作为字符和值,作为其在输入字符串中的出现。
Map countMap = new HashMap();
public void updateCountMap(String inStr, Map<Character, Integer> countMap)
{
char[] chars = inStr.toCharArray();
for(int i=0;i<chars.length;i++)
{
if(!countMap.containsKey(chars[i]))
{
countMap.put(chars[i], 1);
}
countMap.put(chars[i] ,countMap.get(chars[i])+1);
}
return countMap;
}
我们可以做的是逐行读取文件并为每一行调用上面的方法。 每次地图都会不断添加字符的值(出现次数)。 因此,Character数组的大小永远不会太长,我们可以实现我们所需要的。
优点:对输入字符串的字符进行单次迭代。 字符数组大小永远不会增长到高限。 结果图包含每个字符的出现次数。
干杯
您可以使用正则表达式执行此操作:
Pattern pattern = Pattern.compile("[\\(\\)]"); //Pattern says either '(' or ')'
Matcher matcher = pattern.matcher("(Yeahhhh) I have finally made it to the (top)");
int count = 0;
while (matcher.find()) { //call find until nothing is found anymore
count++;
}
System.out.println("count "+count);
Pro是, 模式非常灵活。 您还可以搜索包含的单词: "\\\\(\\\\w+\\\\)"
(A'('后跟一个或多个单词字符,后跟')')
Con是,对于非常简单的情况,它可能就像打破方向盘上的苍蝇一样
我测试了10M字符串的以下方法来计算“,”符号。
// split a string by ","
public static int nof1(String s)
{
int n = 0;
if (s.indexOf(',') > -1)
n = s.split(",").length - 1;
return n;
} // end method nof1
// count "," using char[]
public static int nof2(String s)
{
char[] C = s.toCharArray();
int n = 0;
for (char c : C)
{
if (c == ',')
n++;
} // end for c
return n;
} // end method nof2
// replace "," and calculate difference in length
public static int nof3(String s)
{
String s2 = s.replaceAll(",", "");
return s.length() - s2.length();
} // end method nof3
// count "," using charAt
public static int nof4(String s)
{
int n = 0;
for(int i = 0; i < s.length(); i++)
{
if (',' == s.charAt(i) )
n++;
} // end for i
return n;
} // end method nof4
// count "," using Pattern
public static int nof5(String s)
{
// Pattern pattern = Pattern.compile(","); // compiled outside the method
Matcher matcher = pattern.matcher(s);
int n = 0;
while (matcher.find() )
{
n++;
}
return n;
} // end method nof5
结果:
nof1: 4538 ms
nof2: 474 ms
nof3: 4357 ms
nof4: 357 ms
nof5: 1780 ms
所以,charAt是最快的。 BTW, grep -o ',' | wc -l
grep -o ',' | wc -l
耗时7402 ms。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.