当你有很长的IfElse时,哪一种方法可以做到这一点?IfElse或正则表达式
if (text.contains("text"))
{
// do the thing
}
else if (text.contains("foo"))
{
// do the thing
}
else if (text.contains("bar"))
{
// do the thing
}else ...
或者
if (text.contains("text") || text.contains("foo") || ...)
{
// do the thing
}
或许
Pattern pattern = Pattern.compile("(text)|(foo)|(bar)|...");
Matcher matcher = pattern.matcher(text);
if(matcher.find())
{
// do the thing
}
我的意思是,只有当你必须检查这些很多。谢谢!
我认为它实际上是O(log n),因为一个集合在内部存储为二叉树。尽管如此,HashTable具有O(1)的摊销效率。 – 2012-04-16 13:01:20
从javadoc:“这个类为基本操作(添加,删除,包含和大小)提供了恒定的时间性能,假设散列函数在桶之间正确地分散元素。” - 对于字符串应该是这种情况。 – assylias 2012-04-16 13:02:02
我的不好。我把它们与C++集混淆了,它们“通常作为二叉查找树实现”。 – 2012-04-16 13:06:34