简体   繁体   English

"Java if 条件代码优化"

[英]Java if condition code optimise

Kindly help me if there is any other easy way to optimise\/implement the code on the below possibilities.如果有任何其他简单的方法可以优化\/实现以下可能性的代码,请帮助我。 Variables like home, bill, reg can either be empty string("") or null depending on the scenario. home、bill、reg 等变量可以是空字符串 ("") 或 null,具体取决于场景。

My code<\/strong>我的代码<\/strong>

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");
    }
}

Just check home , bill , and reg individually, because they have no dependency with each other. 只需单独检查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");
    }
}

As proposed just check each variable instead of listing all permutations 按照建议,只需检查每个变量,而不是列出所有排列

    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");
    }
    ...
 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 is " + (StringUtils.isEmpty(home) ? "null" : "not null"));
            System.out.println("bill is " + (StringUtils.isEmpty(bill) ? "null" : "not null"));
            System.out.println("reg is " + (StringUtils.isEmpty(reg) ? "null" : "not null"));
        } else {
            System.out.println("List is null");
        }
    }

According to your comment on other answer, you need to do some actions... 根据您对其他答案的评论,您需要采取一些措施...

So here is an example of how you could reuse the code to do different actions according to the value of different strings: 因此,这里有一个示例,说明如何重用代码根据不同字符串的值执行不同的操作:

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;
}

You could do this for every string, and something quite similar for the list... 您可以为每个字符串执行此操作,并且为列表执行类似的操作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM