2011-06-21 7 views
2

は私が含まれている機能は、次の大文字文字列オブジェクトに一致する関数を含むJava関数で大文字小文字を無視しますか?動作しません。

List<String> pformats= Arrays.asList("odt","ott","oth","odm","sxw","stw","sxg","doc","dot","xml","docx","docm","dotx","dotm","doc","wpd","wps","rtf","txt","csv","sdw","sgl","vor","uot","uof","jtd","jtt","hwp","602","pdb","psw","ods","ots","sxc","stc","xls","xlw","xlt","xlsx","xlsm","xltx","xltm","xlsb","wk1","wks","123","dif","sdc","vor","dbf","slk","uos","pxl","wb2","odp","odg","otp","sxi","sti","ppt","pps","pot","pptx","pptm","potx","potm","sda","sdd","sdp","vor","uop","cgm","bmp","dxf","emf","eps","met","pbm","pct","pcd","pcx","pgm","plt","ppm","psd","ras","sda","sdd","sgf","sgv","svm","tgs","tif","tiff","vor","wmf","xbm","xpm","jpg","jpeg","gif","png","pdf","log"); 

if(pformats.contains(extension)){ 
    // do stuff 
} 
+5

注:HashSetのは、検索のためのより良いです。設定 pformats =新しいHashSetの(は、Arrays.asList(...))。 – Puce

答えて

9

検索のためのより良い選択です。

private static final Set<String> P_FORMATS = new HashSet<String>(Arrays.asList(
    "odt,ott,oth,odm,sxw,stw,sxg,doc,dot,xml,docx,docm,dotx,dotm,doc,wpd,wps,rtf,txt,csv,sdw,sgl,vor,uot,uof,jtd,jtt,hwp,602,pdb,psw,ods,ots,sxc,stc,xls,xlw,xlt,xlsx,xlsm,xltx,xltm,xlsb,wk1,wks,123,dif,sdc,vor,dbf,slk,uos,pxl,wb2,odp,odg,otp,sxi,sti,ppt,pps,pot,pptx,pptm,potx,potm,sda,sdd,sdp,vor,uop,cgm,bmp,dxf,emf,eps,met,pbm,pct,pcd,pcx,pgm,plt,ppm,psd,ras,sda,sdd,sgf,sgv,svm,tgs,tif,tiff,vor,wmf,xbm,xpm,jpg,jpeg,gif,png,pdf,log".split(",")); 

if(P_FORMATS.contains(extension.toLowerCase())){ 
    // do stuff 
} 
6

短い答えである場合でもtrueを返すべきであるとします。これは小文字にあなたの拡張になります、そして、あなたのアレイ内のすべての拡張機能が既に小文字されている場合、それはよより

List<String> pformats= Arrays.asList("odt","ott","oth","odm","sxw","stw","sxg","doc","dot","xml","docx","docm","dotx","dotm","doc","wpd","wps","rtf","txt","csv","sdw","sgl","vor","uot","uof","jtd","jtt","hwp","602","pdb","psw","ods","ots","sxc","stc","xls","xlw","xlt","xlsx","xlsm","xltx","xltm","xlsb","wk1","wks","123","dif","sdc","vor","dbf","slk","uos","pxl","wb2","odp","odg","otp","sxi","sti","ppt","pps","pot","pptx","pptm","potx","potm","sda","sdd","sdp","vor","uop","cgm","bmp","dxf","emf","eps","met","pbm","pct","pcd","pcx","pgm","plt","ppm","psd","ras","sda","sdd","sgf","sgv","svm","tgs","tif","tiff","vor","wmf","xbm","xpm","jpg","jpeg","gif","png","pdf","log"); 
          if(pformats.contains(extension.toLowerCase())){ 

} 

:あなたが私たちの次のコードでき:あなたが含まれている、を上書きすることはできませんが、 wokk。

+0

Peter Lawreyが述べたように、Setは高速になります。 –

1

方法について:

extension.toLowerCase() 

この例ではメソッドが何を行うのかは100%確信がありますが、拡張機能をSetに貼り付ける必要があるかもしれません。

編集:いいえ、特定のオブジェクトが存在するかどうかをcontainsメソッドで確認することはできません。あなたの文字列は、同じ値であっても、別のObjectです。したがって、a)containsメソッドをオーバーライドします。たとえば、配列をループして文字列の比較を行います。またはb)単純に、Setを使用します。

編集2:どうやらそれはArrayList.containsとして以下のコメントごとに動作します(等価性)を確認(あなたが文字列の一致を取得します)が、これはトップに反対しているようだが文句を言わないことを言う答えを可決しました。

+0

'contains()'は 'Collection'にあり、動作するようになっています。 'Set'の方が効率的です。 – artbristol

+0

@artbristol:本当に?私はちょうどその反対を言うために私の答えを編集しました。誰が正しいかわからない! –

+0

@Richard同値、オブジェクトIDのチェックが含まれています。 ArrayListのjavadocは 'このリストに指定された要素が含まれている場合はtrueを返します。より正式には、このリストに(o == null?e == null:o.equals(e))などの少なくとも1つの要素eが含まれている場合にのみtrueを返します。 – Qwerky

1

拡張子のListを正規表現に変換し、CASE_INSENSITVEフラグを付けてコンパイルして使用します。

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public final class Foo { 
    public static void main(final String... args) { 
     final Pattern p = Pattern.compile("odt|ott|oth|odm|sxw|stw|sxg|doc|dot|xml|docx|docm|dotx|dotm|doc|wpd|wps|rtf|txt|csv|sdw|sgl|vor|uot|uof|jtd|jtt|hwp|602|pdb|psw|ods|ots|sxc|stc|xls|xlw|xlt|xlsx|xlsm|xltx|xltm|xlsb|wk1|wks|123|dif|sdc|vor|dbf|slk|uos|pxl|wb2|odp|odg|otp|sxi|sti|ppt|pps|pot|pptx|pptm|potx|potm|sda|sdd|sdp|vor|uop|cgm|bmp|dxf|emf|eps|met|pbm|pct|pcd|pcx|pgm|plt|ppm|psd|ras|sda|sdd|sgf|sgv|svm|tgs|tif|tiff|vor|wmf|xbm|xpm|jpg|jpeg|gif|png|pdf|log", Pattern.CASE_INSENSITIVE); 
     // Will be true 
     System.out.println(p.matcher("bmp").matches()); 
     // Will be false 
     System.out.println(p.matcher("quasar").matches()); 
    } 
} 

あなたがプログラム的に正規表現を構築する場合、これはおそらく維持/読みやすくなるだろうが、私は読者への課題としてあることを残してきました。

1

すべてのフォーマットが下ケースである場合、HashSetのと組み合わせるtoLowerCaseは好ましい解決策です。

大文字と小文字が混在している場合(このようにしておくと、他のものにも同じように使用します)、大文字と小文字を区別しない比較が必要です。

次に、比較器として大文字小文字を区別しないコレータを使用するTreeSet(または他のSortedSet)を実行します。 (これはHashSetほど高速ではありませんが、実際には小さなリストを除いてArrayListよりも高速です)。

また、カスタムhashCodeとequalsを使用するHashSetバリアント(または単にラッパーオブジェクトの通常のHashSet大文字と小文字を区別しないequalsとhashCodeの実装で)うまくいくでしょう。

0

は、この拡張されたListクラスを追加します。

private static class ListIgnoreCase<String> extends java.util.LinkedList { 
     public ListIgnoreCase(Collection<String> c) { 
      super(); 
      addAll(c); 
     } 
     public boolean containsIgnoreCase(java.lang.String toSearch) { 
      for (Object element : this) 
       if (java.lang.String.valueOf(element).equalsIgnoreCase(toSearch)) 
        return true; 

      return false; 
     } 
    } 

今、あなたは、このようasListを呼び出すことができます。

if(new ListIgnoreCase(Arrays.asList("odt","ott","oth","odm")) 
     .containtsIgnoreCase(extension)) { 
     ... 
0

あなたはcollections4(アパッチ)からIteracleUtilsと述語を使用することができます。

List<String> pformats= Arrays.asList("odt","ott","oth","odm","sxw","stw","sxg","doc","dot","xml","docx","docm","dotx","dotm","doc","wpd","wps","rtf","txt","csv","sdw","sgl","vor","uot","uof","jtd","jtt","hwp","602","pdb","psw","ods","ots","sxc","stc","xls","xlw","xlt","xlsx","xlsm","xltx","xltm","xlsb","wk1","wks","123","dif","sdc","vor","dbf","slk","uos","pxl","wb2","odp","odg","otp","sxi","sti","ppt","pps","pot","pptx","pptm","potx","potm","sda","sdd","sdp","vor","uop","cgm","bmp","dxf","emf","eps","met","pbm","pct","pcd","pcx","pgm","plt","ppm","psd","ras","sda","sdd","sgf","sgv","svm","tgs","tif","tiff","vor","wmf","xbm","xpm","jpg","jpeg","gif","png","pdf","log"); 

Predicate<String> predicate = (s) -> StringUtils.equalsIgnoreCase(s, "JPG"); 
if(IterableUtils.matchesAny(pformats, predicate)) 
    // do stuff 
} 

org.apache.commons.collections4.IterableUtils

関連する問題