如何在JAVA中结合两种方法?

时间:2016-06-14 19:44:24

标签: java java-8 boolean

我有两个返回两个值的方法。该方法大致相同,所以我想将它们组合到单个方法并操纵返回不同的值但不确定它是否有用?

有人能告诉我这个方法是否可以转换成单一的?

private boolean getFirst(List<Apples> apples) {

        boolean isOrange  = false;
        if (apples != null) {
            for (Apples apple : apples) {
                String type = apple.getFruit();
                boolean isApple = StringUtils.equalsIgnoreCase(type, ORANGE);
                if (!isApple) {
                    isOrange = true;
                }
            }
        }
        return isOrange;
    }



 private boolean getSecond(List<Apples> apples) {

        boolean isAppletype  = false;
        if (apples != null) {
            for (Apples apple : apples) {
                 String type = apple.getFruit();
                boolean isApple = StringUtils.equalsIgnoreCase(type, ORANGE);
                 if (isApple) {
                    isAppletype = true;
                }
            }
        }
        return isAppletype;
    }

3 个答案:

答案 0 :(得分:4)

您可以使用流:

List<Apple> list = ...;

// First method
list.stream().anyMatch((e) -> !StringUtils.equalsIgnoreCase(e.getFruit(), ORANGE));
// Second method
list.stream().anyMatch((e) -> StringUtils.equalsIgnoreCase(e.getFruit(), ORANGE));

答案 1 :(得分:1)

是的,你可以肯定地将这些方法合并到一个新的方法中,它可以更通用...

例如,如果我将方法重命名为 isThisFruitPresentInTheList ,请原谅我使用的名称约定:) ...

然后你可以将一个列表传递给方法,作为第二个参数你要找的水果,如果列表中有水果,方法将返回true,否则返回false ...

实施例

print clean
1.52, -59.49%, 0.93, 0.17, 800, "1.51 - 1.59", 1.54, 1.59, N/A, -0.01, +5.25%, 0.16, N/A, 0.08, -2.32, N/A, 6.90M, -1.24, N/A, N/A, 1.50, 100, 326754, N/A, N/A, N/A, N/A, 1.51, "NYQ", N/A, "0.93 - 3.90", N/A, "3:48pm - <b>1.58</b>", "Ciber, "+3.95%", N/A, 3700, N/A, N/A, -34.46%, N/A, 2.84, N/A, N/A, N/A, 0.65, N/A, 127.47M, N/A, +69.89%, 2.63, N/A, N/A, 693036, 1.58, -0.65, 73289000, N/A, N/A, -0.83, 0.53, 80680000, N/A, N/A, N/A,  Inc. Common Stock", 3.90, "6/14/2016", -0.16, N/A, N/A, 760.00M, +0.06, N/A, N/A, N/A, 2.41, "3:48pm", N/A, "+0.06 - +3.95%", 326754

print query_string
INSERT IGNORE INTO YAHOO VALUES ("1.52, -59.49%, 0.93, 0.17, 800, "1.51 - 1.59", 1.54, 1.59, N/A, -0.01, +5.25%, 0.16, N/A, 0.08, -2.32, N/A, 6.90M, -1.24, N/A, N/A, 1.50, 100, 335969, N/A, N/A, N/A, N/A, 1.51, "NYQ", N/A, "0.93 - 3.90", N/A, "3:51pm - <b>1.58</b>", "Ciber, "+3.95%", N/A, 3700, N/A, N/A, -34.46%, N/A, 2.84, N/A, N/A, N/A, 0.65, N/A, 127.47M, N/A, +69.89%, 2.63, N/A, N/A, 693036, 1.58, -0.65, 73289000, N/A, N/A, -0.83, 0.53, 80680000, N/A, N/A, N/A,  Inc. Common Stock", 3.90, "6/14/2016", -0.16, N/A, N/A, 760.00M, +0.06, N/A, N/A, N/A, 2.41, "3:51pm", N/A, "+0.06 - +3.95%", 335969");

你可以像do ....一样调用方法。

 private boolean isThisFruitPresentInTheList(List<Apples> apples, String f) {
        if (apples != null) {
            for (Apples apple : apples) {
                if (f.equalsIgnoreCase(apple.getFruit())) {
                    return true;
                }
            }
        }
        return false;
    }

答案 2 :(得分:0)

鉴于

private boolean containsType(List<Apples> apples, boolean orangeType) {
    if (apples != null) {
        for (Apples apple : apples) {
            String type = apple.getFruit();
            boolean isOrange = StringUtils.equalsIgnoreCase(type, ORANGE);
            if (orangeType == isOrange)
                return true;
        }
    }
    return false;
}

您的方法将等同于以下

  • getFirst(apples) =&gt; containsType(apples, false)
  • getSecond(apples) =&gt; containsType(apples, true)