如何在java中检查多维字符串数组中包含的值

时间:2023-01-31 07:29:24

how to check a value contain in multi dimensional string array in java 8 .

如何在java 8中检查多维字符串数组中包含的值。

So far i have been using this,

到目前为止我一直在用这个,

public  boolean exists(String value) {

    String[][] actType=Helper.getTypes();
    for(int i = 0; i< actType.length; i++){
        for (int j = 0; j<actType[i].length; j++){
            if(actType[i][j].equals(value)){
                return true; 
            }
        }
    }
    return false;
}

I want to know, is there any java 8 methods

我想知道,有没有任何java 8方法

1 个解决方案

#1


1  

You can use nested Stream.of(T values...) to turn your array to a stream and then use method references:

您可以使用嵌套的Stream.of(T值...)将数组转换为流,然后使用方法引用:

public  boolean exists(String value) {
    String[][] actType=Helper.getTypes();
    return Stream.of(actType).flatMap(Stream::of).anyMatch(value::equals);
}

Stream.of(actType) will get you a stream of String[] and flatMap in combination with another Stream::of will get you a stream of String. If any of those strings equal your value anyMatch will return true.

Stream.of(actType)将为您提供String []流和flatMap与另一个Stream :: of的组合,它将为您提供String流。如果这些字符串中的任何一个等于您的值,则anyMatch将返回true。

#1


1  

You can use nested Stream.of(T values...) to turn your array to a stream and then use method references:

您可以使用嵌套的Stream.of(T值...)将数组转换为流,然后使用方法引用:

public  boolean exists(String value) {
    String[][] actType=Helper.getTypes();
    return Stream.of(actType).flatMap(Stream::of).anyMatch(value::equals);
}

Stream.of(actType) will get you a stream of String[] and flatMap in combination with another Stream::of will get you a stream of String. If any of those strings equal your value anyMatch will return true.

Stream.of(actType)将为您提供String []流和flatMap与另一个Stream :: of的组合,它将为您提供String流。如果这些字符串中的任何一个等于您的值,则anyMatch将返回true。