今天早上我感到好奇,是遊蕩,如果有人有更好的方法來做到這一點的Java:字符串:有沒有更好的方法來比較字符串
if(TAG_PLAY.equalsIgnoreCase(e.getActionCommand())
||TAG_PASSWORD.equalsIgnoreCase(e.getActionCommand())
||...
){
我有一種預感,這可以提高通過執行類似 創建1個大串,並在其中 尋找e.getActionCommand(),但我不知道它是否會更有效
注:此無關與getActionCommand,我純粹感興趣的邏輯,性能和新的方法/模式做同樣的事情
編輯:我不考慮大小寫的辯論^^
編輯:
如何:
s = TAG_PLAY+","+TAG_PASSWORD;
//compareToIgnoreCase is not optimal since it will go through all the String
if(0!=s.compareToIgnoreCase(anotherString)){
如果將保證不會重複添加(或即使是 - 無論在這裏作爲'contains'只需要找到一個),那麼'[Array] List'也會起作用。一個 「優勢」 使用列表(對比組)是'Arrays.asList(TAG_PLAY.toLowerCase(),TAG_PADDWORD,....)'。就個人而言,我被拖放到'toLowerCase'的重複,但Java遺憾地缺乏[n簡單]「地圖」構造。對於微不足道的n,O(n)與O(1)辯論在這裏基本上是無關緊要的。 – 2010-12-15 04:31:12
你也可以在一個語句中創建一個集合 - 新的HashSet(Arrays.asList(TAG_PLAY.toLowerCase(),TAG_PASSWORD.toLowerCase(),....)))。 – 2010-12-15 05:08:44