Java条件代码优化

时间:2015-07-09 15:24:53

标签: java if-statement java-7 notnull

如果有其他简单方法可以优化/实现以下可能性的代码,请帮助我。 home,bill,reg等变量可以是空字符串(""),也可以为null,具体取决于方案。

我的代码

public void helloWorld()
{
    List items=new ArrayList();
    items.add("1"); // dummy list
    String home=""; // can be null or empty
    String bill=null;  // can be null or empty
    String reg="";  // can be null or empty
    if(!items.isEmpty())
    {
        System.out.println("List is not null");
        if(home==null&&bill==null&&reg==null) 
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home==null&&bill==null&&reg!=null)
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is not null");
        }
        if(home==null&&bill!=null&&reg==null)
        {
            System.out.println("home is null");
            System.out.println("bill is not null");
            System.out.println("reg is null");
        }
        if(home==null&&bill!=null&&reg!=null)
        {
            System.out.println("home is null");
            System.out.println("bill is not null");
            System.out.println("reg is not null");
        }
        if(home!=null&&bill==null&&reg==null)
        {
            System.out.println("home is not null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home!=null&&bill==null&&reg!=null)
        {
            System.out.println("home is not null");
            System.out.println("bill is null");
            System.out.println("reg is not null");
        }
        if(home!=null&&bill!=null&&reg==null)
        {
            System.out.println("home is not null");
            System.out.println("bill is not null");
            System.out.println("reg is null");
        }
        if(home!=null&&bill!=null&&reg!=null)
        {
            System.out.println("home is not null");
            System.out.println("bill is not null");
            System.out.println("reg is not null");
        }
    }
    else
    {
        System.out.println("List is null");
    }
}

请指教。 提前谢谢。

3 个答案:

答案 0 :(得分:3)

只需单独检查homebillreg,因为它们之间没有任何依赖关系。

public void helloWorld()
{
    List items=new ArrayList();
    items.add("1"); // dummy list
    String home=""; // can be null or empty
    String bill=null;  // can be null or empty
    String reg="";  // can be null or empty
    if(!items.isEmpty())
    {
        System.out.println("List is not null");
        System.out.println( home == null ? "home is null" : "home is not null" );
        System.out.println( bill == null ? "bill is null" : "bill is not null" );
        System.out.println( reg == null ? "reg is null" : "reg is not null" );

    }
    else
    {
        System.out.println("List is null");
    }
}

答案 1 :(得分:1)

建议只检查每个变量而不是列出所有排列

    if(home==null) 
    {
        System.out.println("home is null");
    }
    else
    {
        System.out.println("home is not null");
    }
    if (bill==null)
    {
        System.out.println("bill is null");
    }
    else
    {
        System.out.println("bill is not null");
    }
    ...

答案 2 :(得分:0)

根据您对其他答案的评论,您需要采取一些行动......

以下是一个示例,说明如何根据不同字符串的值重用代码来执行不同的操作:

public void helloWorld() {
    List items = new ArrayList();
    items.add("1"); // dummy list
    String home = ""; // can be null or empty
    String bill = null; // can be null or empty
    String reg = ""; // can be null or empty


    switch (validateInputString(home)) {
    case 1:
        //doAction 1;
        break;
    case 2:
        //doAction 2;
        break;
    case 3:
        //doAction 3;
        break;

    default:
        break;
    }

}

public int validateInputString(String toValidate){
    if(toValidate==null)
        return 1;

    if(toValidate.equals(""))
        return 2;


    return 3;
}

你可以为每个字符串执行此操作,并为列表提供类似的内容...