當你有很長的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