在Java中避免使用多个If语句

jai*_*jai 10 java

我编写了一个像这样的方法.但我想这应该进行重构.任何人都可以建议避免使用这多个if语句的最佳方法吗?

private String getMimeType(String fileName){
  if(fileName == null) {
    return "";   
  } 
  if(fileName.endsWith(".pdf")) {
    return "application/pdf";   
  }
  if(fileName.endsWith(".doc")) {
    return "application/msword";  
  }
  if(fileName.endsWith(".xls")) {
    return "application/vnd.ms-excel"; 
  }
  if(fileName.endsWith(".xlw")) {
    return "application/vnd.ms-excel"; 
  }
  if(fileName.endsWith(".ppt")) {
    return "application/vnd.ms-powerpoint"; 
  }
  if(fileName.endsWith(".mdb")) {
    return "application/x-msaccess"; 
  }
  if(fileName.endsWith(".rtf")) {
    return "application/rtf"; 
  }
  if(fileName.endsWith(".txt")) {
    return "txt/plain"; 
  }
  if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
    return "txt/html"; 
  }
  return "txt/plain"; 
}
Run Code Online (Sandbox Code Playgroud)

我不能在这里使用switch-case,因为我的'条件'是java.lang.String.

Joa*_*uer 34

您可以使用a Map来保存您的解决方案:

Map<String,String> extensionToMimeType = new HashMap<String,String>();
extensionToMimeType.put("pdf", "application/pdf");
extensionToMimeType.put("doc", "application/msword");
// and the rest

int lastDot = fileName.lastIndexOf(".");
String mimeType;
if (lastDot==-1) {
    mimeType = NO_EXTENSION_MIME_TYPE;
} else {
    String extension = fileName.substring(lastDot+1);
    mimeType = extensionToMimeType.get(extension);
    if (mimeType == null) {
        mimeType = UNKNOWN_EXTENSION_MIME_TYPE;
    }
}
Run Code Online (Sandbox Code Playgroud)

为使该代码工作,你需要定义NO_EXTENSION_MIME_TYPEUNKNOWN_EXTENSION_MIME_TYPE为您的课,有点像这样:

private static final String NO_EXTENSION_MIME_TYPE = "application/octet-stream";
private static final String UNKNOWN_EXTENSION_MIME_TYPE = "text/plain";
Run Code Online (Sandbox Code Playgroud)


Kez*_*zer 11

或许使用HashMap

这样你就可以做到 myMap.get(mystr);

  • @dfa:我认为我们可以假设OP有智能来弄清楚如何将后缀作为String. (3认同)

nxh*_*oaf 5

命令模式是要走的路。下面是一个使用 java 8 的例子:

1.定义接口:

public interface ExtensionHandler {
  boolean isMatched(String fileName);
  String handle(String fileName);
}
Run Code Online (Sandbox Code Playgroud)

2. 用每个扩展实现接口:

public class PdfHandler implements ExtensionHandler {
  @Override
  public boolean isMatched(String fileName) {
    return fileName.endsWith(".pdf");
  }

  @Override
  public String handle(String fileName) {
    return "application/pdf";
  }
}
Run Code Online (Sandbox Code Playgroud)

public class TxtHandler implements ExtensionHandler {
  @Override public boolean isMatched(String fileName) {
    return fileName.endsWith(".txt");
  }

  @Override public String handle(String fileName) {
    return "txt/plain";
  }
}
Run Code Online (Sandbox Code Playgroud)

等等 .....

3. 定义客户端:

public class MimeTypeGetter {
  private List<ExtensionHandler> extensionHandlers;
  private ExtensionHandler plainTextHandler;

  public MimeTypeGetter() {
    extensionHandlers = new ArrayList<>();

    extensionHandlers.add(new PdfHandler());
    extensionHandlers.add(new DocHandler());
    extensionHandlers.add(new XlsHandler());

    // and so on

    plainTextHandler = new PlainTextHandler();
    extensionHandlers.add(plainTextHandler);
  }

  public String getMimeType(String fileExtension) {
    return extensionHandlers.stream()
      .filter(handler -> handler.isMatched(fileExtension))
      .findFirst()
      .orElse(plainTextHandler)
      .handle(fileExtension);
  }
}
Run Code Online (Sandbox Code Playgroud)

4. 这是示例结果:

  public static void main(String[] args) {
    MimeTypeGetter mimeTypeGetter = new MimeTypeGetter();

    System.out.println(mimeTypeGetter.getMimeType("test.pdf")); // application/pdf
    System.out.println(mimeTypeGetter.getMimeType("hello.txt")); // txt/plain
    System.out.println(mimeTypeGetter.getMimeType("my presentation.ppt")); // "application/vnd.ms-powerpoint"
  }
Run Code Online (Sandbox Code Playgroud)