寻求帮助创建更有效的方法来进行大量检查

时间:2014-12-26 01:58:35

标签: java

注意:不是How do I compare strings in java的副本,因为我正在考虑通过一些检查来确定某些东西是否继承了其他东西

他们是一种更好,更有效的方法吗?

正如您所看到的,我输入2个字符串然后在列表中检查它们,就好像current = 3然后它返回true以检查一个,两个和三个

注意:这些值(一,二,三)只是我在使用中的示例的占位符,它们之间没有关系,只是它们具有不同的优先级。

public boolean checker(String current, String check) {


    if (check.equals("one")) {
        if (current.equals("one") || current.equals("two")
                || current.equals("three")) {
            return true;
        }
    }
    if (check.equals("two")) {
        if (current.equals("two") || current.equals("three")) {
            return true;
        }
    }
    if (check.equals("three")) {
        if (current.equals("three")) {
            return true;
        }
    }
    return false;

}

3 个答案:

答案 0 :(得分:0)

以下是一些指示

  1. 正如Frisch在评论中提到的那样,使用.equals而不是==进行字符串比较。

  2. 使用开关/案例

    switch (check) {
        case "one":
            if (current.equals("one")) return true;
        case "two":
            if (current.equals("two")) return true;
        case "three":
            if (current.equals("three")) return true;
    }
    
  3. 除此之外,似乎没什么可做的。

答案 1 :(得分:0)

两件事。

  1. 不要使用相等性检查字符串。使用.equals()方法。你可以从字符串文字中调用它。所以这样的事情。即使使用空值,将其从字符串文字中调用也是安全的,这通常是一件好事。

    if ("one".equals(check))
    
  2. 您可以利用Java的短路运算符&&||

    if ("one".equals(check)) {
        if ("one".equals(current) || "two".equals(current) || "three".equals(current)) {
            return true;
        }
    }
    
  3. 可以成为

    if ("one".equals(check) && ("one".equals(current) || "two".equals(current) || "three".equals(current))) {
        return true;
    }
    

    将从左到右进行评估。由于“one”.equals(check)位于最左边,并且与语句的其余部分相对应,因此Java将退出条件检查“one”.equals(check)是否为不是真的,也不会评估声明的其余部分。

    由于您只是返回true或false,您还可以更进一步,使用De Morgan定律(http://en.wikipedia.org/wiki/De_Morgan's_laws)将所有if语句缩减为单个语句。通常你以最自然的方式陈述你的boolean if语句,然后你开始通过应用保持逻辑if语句相同的转换来简化它。

    这方面的一个很好的例子是,从以下链接中窃取。

    在main方法的程序体的上下文中,假设定义了以下数据:

    int score, min = 0, max = 20;
    boolean bad, good;
    

    进一步假设可以从键盘输入中为分数分配值,并且我想用布尔表达式测试分数是否是有效数字。好的分数在闭合范围[0 .. 20],包括0和20。

    good = (score >= min && score <= max);
    
    I would like to get the score from the keyboard in a do while loop, so that I can validate the entry. The logic in my control structure is to demand another entry for the score while the entry is bad. I have a definition of a good entry, and I will use definitions of operators and De Morgan's Law to help me write an expression that represents a bad entry.
    
    good = (score >= min && score <= max); // original definition of good from the logic of my task 
    good = !(score < min) && !(score > max); // by definition, >= means ! < , <= means ! >
    good = !(score < min || score > max); // by De Morgan's' Law
    bad = !good ; // bad is not good
    bad = !!(score < min || score > max); // substituting for good
    bad = score < min || score > max; // double negation is dropped
    

    http://fcmail.aisd.net/~JABEL/1DeMorgansLaw.htm

答案 2 :(得分:0)

我想更新一些事情。 1.我们只能在原始数据类型上应用switch case,但不能在对象上应用switch case。由于字符串是对象,我们不能在case / switch语句中使用字符串。

在这种情况下,我建议您使用枚举/地图。

请查看下面的示例程序,我是如何使用地图实现的。

 public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);

        String current = "one";
        String check = "one";
        if(map.get(check)<=map.get(current)){
            System.out.println("Our condition is success");
        }
    }

而不是多重比较,这更好。

--- Santhosh