有没有更好的方法从多个字符串数组中查找任何字符串

时间:2018-10-24 08:13:07

标签: java string arraylist

在下面的示例中,我在下面的示例中提到了一些预定义的String数组,现在我的要求是,如果每个数组中的boName都存在,我需要调用为特定自定义文件定义的方法。还有其他方法可以避免多个for循环。

public class MyClass {

    private static final String[] OTHER_CUSTOM_FIELD_RELATED_BO_NAME = { "A", "B", "C", "D", "E", "F", "G", "H" };
    private static final String[] TRANSACTION_CUSTOM_FIELD_RELATED_BO_NAME = { "A", "AB", "CD", "DE", "EG" };
    private static final String[] CRM_CUSTOM_FIELD_RELATED_BO_NAME = { "I", "J", "K", "L", "M" };
    private static final String[] ITEM_CUSTOM_FIELD_RELATED_BO_NAME = { "N", "O", "P", "Q" };
    private static final String[] ENTITY_CUSTOM_FIELD_RELATED_BO_NAME = { "AN", "ZY", "X", "P" };

    public static List<String> BusinessObjectDescribe(String boName) {
        List<String> fields = new ArrayList<String>();
        if (boName != null && !boName.isEmpty()) {
            for (String string : OTHER_CUSTOM_FIELD_RELATED_BO_NAME) {
                if (boName.equals(string)) {
                    fields.addAll(getOtherCustomFields());
                    break;
                }
            }

            for (String string : TRANSACTION_CUSTOM_FIELD_RELATED_BO_NAME) {
                if (boName.equals(string)) {
                    fields.addAll(getTransactionCustomFields());
                    break;
                }
            }

            for (String string : CRM_CUSTOM_FIELD_RELATED_BO_NAME) {
                if (boName.equals(string)) {
                    fields.addAll(getCRMCustomFields());
                    break;
                }
            }

            for (String string : ITEM_CUSTOM_FIELD_RELATED_BO_NAME) {
                if (boName.equals(string)) {
                    fields.addAll(getITEMCustomFields());
                    break;
                }
            }

            for (String string : ENTITY_CUSTOM_FIELD_RELATED_BO_NAME) {
                if (boName.equals(string)) {
                    fields.addAll(getEntityCustomFields());
                    break;
                }
            }

        }

        return fields;

    }

}

5 个答案:

答案 0 :(得分:2)

如果您正在使用Java8,则可以利用功能接口的优势。只需创建新函数即可,将您的数组作为第一个参数,将更高版本的函数作为第二个参数。在更高级的版本中,您可以传递有关添加的逻辑。而且您需要多次调用该函数,而不是重复代码。

答案 1 :(得分:1)

要进行搜索,可以在java-8中使用以下内容:

Arrays.stream(OTHER_CUSTOM_FIELD_RELATED_BO_NAME)
    .filter(e -> e.contains("A"))
    .findFirst();

在这里,我们将数组转换为流,然后应用过滤器。如果找到匹配项,它将返回该元素,否则将返回null。另外,.findFirst()将在找到匹配项后立即停止搜索流。

您可以使用相同的语法并根据需要搜索其他数组。

答案 2 :(得分:0)

我将创建一个类,该类封装您在循环中执行的行为。

class MyClass {
    class BoNameHandler {
        private Collection<String> applicableNames;
        private Supplier<Collection<String>> getter;
        BoNameHandler(String[] names, Supplier<Collection<String>> g) {
            applicableNames = Arrays.asList(names);
            getter = g;
        }
        void addIfApplicable(String boName, List<String> addTo) {
            if (applicableNames.stream().filter(boName::equals).findFirst().isPresent()) {
                addTo.addAll(getter.get());
            }
        }
    }
    List<BoNameHandler> handlers = new ArrayList<>();
    MyClass() {
        handlers.add(new BoNameHandler(OTHER_CUSTOM_FIELD_RELATED_BO_NAME, this::getOtherCustomFields));
        handlers.add(new BoNameHandler(TRANSACTION_CUSTOM_FIELD_RELATED_BO_NAME, this::getTransactionCustomFields));
        // and the others
    }
}

这会将您的代码减少为

public static List<String> BusinessObjectDescribe(String boName) {
    List<String> fields = new ArrayList<String>();
    for (BoNameHandler handler : handlers) {
        handler.addIfApplicable(boName, fields);
    }
    return fields;
}

我不知道您的方法是什么样子,但是如果它们与BO_NAME一样固定,那么您也可以将它们组合成一个enum

答案 3 :(得分:0)

Set一样,数学集{ a, b, c }实际上是最逻辑的数据结构,使用它。

private static final Set<String> OTHER_CUSTOM_FIELD_RELATED_BO_NAME =
    Set.of( "A", "B", "C", "D", "E", "F", "G", "H" );
private static final Set<String> TRANSACTION_CUSTOM_FIELD_RELATED_BO_NAME =
    Set.of( "A", "AB", "CD", "DE", "EG" );
private static final Set<String> CRM_CUSTOM_FIELD_RELATED_BO_NAME =
    Set.of( "I", "J", "K", "L", "M" );
private static final Set<String> ITEM_CUSTOM_FIELD_RELATED_BO_NAME =
    Set.of( "N", "O", "P", "Q" );
private static final Set<String> ENTITY_CUSTOM_FIELD_RELATED_BO_NAME =
    Set.of( "AN", "ZY", "X", "P" );


public static List<String> BusinessObjectDescribe(String boName) {
    List<String> fields = new ArrayList<>();
    if (boName != null && !boName.isEmpty()) {
        if (OTHER_CUSTOM_FIELD_RELATED_BO_NAME.contains(boName)) {
            fields.addAll(getOtherCustomFields());
        }
        if (TRANSACTION_CUSTOM_FIELD_RELATED_BO_NAME.contains(boName)) {
            fields.addAll(getTransactionCustomFields());
        }
        if (CRM_CUSTOM_FIELD_RELATED_BO_NAME.contains(boName)) {
            fields.addAll(getCRMCustomFields());
        }
        if (ITEM_CUSTOM_FIELD_RELATED_BO_NAME.contains(boName)) {
            fields.addAll(getITEMCustomFields());
        }
        if (ENTITY_CUSTOM_FIELD_RELATED_BO_NAME.contains(boName)) {
            fields.addAll(getEntityCustomFields());
        }
    }
    return fields;
}

如果您的Java版本仍然不知道Set.of (在Java 9之前),请执行以下操作:

private static final Set<String> OTHER_CUSTOM_FIELD_RELATED_BO_NAME = new HashSet<>();
static {
    Collections.addAll(OTHER_CUSTOM_FIELD_RELATED_BO_NAME,
        "A", "B", "C", "D", "E", "F", "G", "H" );
}

Set是最快的数据结构。那么为什么发挥最好的作用。

答案 4 :(得分:0)

我还考虑将所有这些数组放入一个Map或Map中,然后进行处理。