Abbreviation if () with the string.equals method

Is there a way to shorten this if() ? To avoid repeating string.equals() somehow?

 if (extension.equals("jpg") || extension.equals("JPG") || extension.equals("png") || extension.equals("PNG") || extension.equals("bmp") || extension.equals("BMP") || extension.equals("jpeg") || extension.equals("JPEG")) { tmp.setIcon(new ImageIcon(getClass().getResource("/menage/Resources/imageIco.png"))); } 

Something like this:

 if (extension.equals(("jpg")||("JPG")||("png")||("PNG")||("bmp")||("BMP")||("jpeg")||("JPEG"))) { tmp.setIcon(new ImageIcon(getClass().getResource("/menage/Resources/imageIco.png"));) } 

I know this question looks strange, however if() with such a list of long conditions is unclear and requires a lot of writing.

+5
source share
8 answers

Start by changing equals(...) to equalsIgnoreCase(...) .

Other options, create a HashSet of lowercase letters (or uppercase if desired) with your image extensions and see if it contains your string of interest, changed to lowercase:

 if (imageExtSet.contains(myExtension.toLowerCase()) { } 
+5
source

Here is a short version with predefined image types:

 Set<String> imgTypes = new HashSet<>() {{ add("jpg"); add("JPG"); add("png"); add("PNG"); add("bmp"); add("BMP"); add("jpeg"); add("JPEG"); }}; public boolean isImgType(String type) { return imgTypes.contains(type); } 
+4
source

You can save all the values ​​in a list and then ask if it contains. If this is only one liner (you do not need to request this condition elsewhere), you can do:

 if (Arrays.asList("jpg", "JPG", "png", "PNG", "bmp", "BMP", "jpeg", "JPEG").contains(extension)) 

Of course, you can save the list as an object, and then at any place where you need to request this condition.

+3
source

Use HashSet Like This

 Set<String> extSet= new HashSet<String>(); // Add All in Lower case .. to save your efforts extSet.add("jpg"); extSet.add("png"); //...etc etc 

and just check if it is present in Set

 if(extSet.contains(extension==null?null:extension.toLowerCase())) { /// True } else { // False } 
+2
source

One thing you can do to eliminate some checks is to convert the string to lowercase:

 String ext = extension.toLowerCase(); 

Now you have reduced the statement to:

 if (ext.equals("jpg") || ext.equals("png") || ext.equals("bmp") || ext.equals("jpeg")) 
0
source
 if (Arrays.asList("jpg", "jpeg", "png", "bmp").contains(extension.toLowerCase)) 
0
source

Add some methods ...

 private static boolean isJpeg(String ext) { return java.util.Arrays.asList("jpg", "jpeg").contains(ext.toLowerCase()); } private static boolean isPng(String ext) { return "png".equalsIgnoreCase(ext); } private static boolean isBmp(String ext) { return "bmp".equalsIgnoreCase(ext); } 

And change it to ...

 else if (isJpeg(extension) || isPng(extension) || isBmp(extension)) { tmp.setIcon(new ImageIcon(getClass().getResource("/menage/Resources/imageIco.png"))); } 

isJpeg will isJpeg NullPointerException if the extension is null, so make sure it is not null by adding extension != null || ... extension != null || ... or something like that.


The above is slightly different for your specific case, as it allows JpEg and all other mixed capitalizations to slip. If you do not want this, use them. In addition, the following has the added benefit of never throwing a NullPointerException if the extension is null .

 private static boolean isJpeg(String ext) { return java.util.Arrays.asList("jpg", "JPG", "jpeg", "JPEG").contains(ext); } private static boolean isPng(String ext) { return java.util.Arrays.asList("png", "PNG").contains(ext); } private static boolean isBmp(String ext) { return java.util.Arrays.asList("bmp", "BMP").contains(ext); } 
0
source

Other answers give a lot of good low-level ideas, but the basic principle here is to prevent code reuse.

If you run this test several times, create a method that runs the test for you:

 boolean isValidImageExtenstion(String extension) { return (extension.equals("jpg") || extension.equals("JPG") || extension.equals("png") || extension.equals("PNG") || extension.equals("bmp") || extension.equals("BMP") || extension.equals("jpeg") || extension.equals("JPEG")); } 

Call the method when you need it. If you like, you can use one of the approaches described in the other answers in the method (and the suggestion to “ignore cases” is definitely worth it), but the rest becomes less important when you prevent code repetition. As a bonus, if you decide you want to support gif extensions, you only need to make changes in one place.

The advantages of this approach over others are that it is self-documenting. This is pretty obvious what this method does, and some other answers are pretty obscure.

If you do this only once and are not going to do it again, then you have already created the working code, so do not waste time changing the working code .

0
source

All Articles