简体   繁体   English

用返回类型调用方法的Java最佳实践

[英]Java best practice for calling method with return type

How can I avoid calling the same method for three times, what's the best practice in this kind of situation? 如何避免重复调用同一方法三遍,在这种情况下的最佳实践是什么? Or is it the right way? 还是正确的方法?

I'm checking the list is not empty first and I'm checking it again to make sure is not empty because do not wants to print empty list. 我先检查列表不为空,然后再次检查以确保不为空,因为不想打印空列表。

public class RegExpTest_1 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("item1");
        list.add("#item2");
        list.add("item3");
        list.add("&item4");
        list.add("item5");

        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("item1");
        list1.add("item2");
        list1.add("item3");
        list1.add("item4");
        list1.add("item5");

        if (StringUtils.isNotEmpty(finditem(list)) || StringUtils.isNotEmpty(finditem(list1))){  // calling method
            if (StringUtils.isNotEmpty(finditem(list))) {         //calling same method
                System.out.println("List :\n" + finditem(list));  //calling same method
            }
            if (StringUtils.isNotEmpty(finditem(list1))) {
                System.out.println("List :\n" + finditem(list1));
            }
        }
    } //main

    public static String finditem(ArrayList<String> alist){
        StringBuilder sb = new StringBuilder();
        String re1=".*?";   // Non-greedy match on filler
        String re2="(^#|^&)";   // Word 1
        Pattern p = Pattern.compile(re1+re2,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

        for (String str:alist) {
            Matcher m = p.matcher(str);
            if (m.find()) {
              //  System.out.println("found" + m.group(1));
                sb.append(str);
                sb.append("\n");
            } else {
               // System.out.print("not found");
            }
        }

        return sb.toString();
    }
}

The outer if-statement is redundant. 外部if语句是多余的。 Just use the two inner if-statements. 只需使用两个内部if语句。

eg 例如

    if (StringUtils.isNotEmpty(finditem(list))) {         //calling same method
        System.out.println("List :\n" + finditem(list));  //calling same method

    }
    if (StringUtils.isNotEmpty(finditem(list1))) {
        System.out.println("List :\n" + finditem(list1));
    }

You can just use this construction: 您可以使用以下结构:

if (StringUtils.isNotEmpty(finditem(list))) { 
    System.out.println("List :\n" + finditem(list)); 

}
if (StringUtils.isNotEmpty(finditem(list1))) {
    System.out.println("List :\n" + finditem(list1));
}

I think that external 'if' is redundant. 我认为外部“如果”是多余的。

If you're using Java 8, it's a really easy implementation. 如果您使用的是Java 8,这是一个非常简单的实现。 With the advantage that you may modify this and add as much lists as you want just by adding them within Stream.of 优点是您可以修改此列表并根据需要添加Stream.of数量的列表,只需将它们添加到Stream.of

  1. Make a stream of out the lists you want to finditems within 列出您要在其中找到项目的列表
  2. Map the lists to a String by giving them as parameter to finditem 通过将列表作为finditem参数将列表映射到String
  3. Check if the String is empty, if it is, it doesn't continue the Stream pipeline 检查String是否为空,如果为空,则不继续Stream管道
  4. Print to the console only if the returned string is not empty of course. 当然,仅在返回的字符串不为空时才打印到控制台。

Stream.of(list, list1)
      .map(RegExpTest_1::finditem)
      .filter(x -> !x.isEmpty())
      .forEach(items -> System.out.println("List : \n" + items));

Your target is to print out non-empty list content, so the more better is to change your finditem method and adjust your calling methods for better structural and performance. 您的目标是打印非空列表内容,因此更好的办法是更改finditem方法并调整调用方法,以提高结构和性能。

public class RegExpTest_1 {

public static void main(String[] args) {


    ArrayList<String> list = new ArrayList<String>();
    list.add("item1");
    list.add("#item2");
    list.add("item3");
    list.add("&item4");
    list.add("item5");

    ArrayList<String> list1 = new ArrayList<String>();
    list1.add("item1");
    list1.add("item2");
    list1.add("item3");
    list1.add("item4");
    list1.add("item5");

    /*
    if (StringUtils.isNotEmpty(finditem(list)) || StringUtils.isNotEmpty(finditem(list1))) {  // calling method
        if (StringUtils.isNotEmpty(finditem(list))) {         //calling same method
            System.out.println("List :\n" + finditem(list));  //calling same method

        }
        if (StringUtils.isNotEmpty(finditem(list1))) {
            System.out.println("List :\n" + finditem(list1));
        }
    }
    */

    final String s = finditem(list);
    if (StringUtils.isNotEmpty(s)) {
        System.out.println("List :\n" + s);
    }

    final String s1 = finditem(list1);
    if (StringUtils.isNotEmpty(s1)) {
        System.out.println("List :\n" + s1);
    }


} //main

public static String finditem(ArrayList<String> alist) {
    // Returns empty string directly if alist is null or empty
    if (null == alist || 0 == alist.size()) {
        return "";
    }

    StringBuilder sb = new StringBuilder();
    String re1 = ".*?";   // Non-greedy match on filler
    String re2 = "(^#|^&)";   // Word 1
    Pattern p = Pattern.compile(re1 + re2, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    for (String str : alist) {

        Matcher m = p.matcher(str);
        if (m.find()) {
            //  System.out.println("found" + m.group(1));
            sb.append(str);
            sb.append("\n");
        } else {
            // System.out.print("not found");
        }
    }

    return sb.toString();
}

final static class StringUtils {
    // Do your StringUtils.isNotEmpty's do :)
    public static boolean isNotEmpty(final String s) {
        return (s != null && s.length() > 0);
    }
}
}

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

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