如果字符串包含列表中的任何项,JAVA将返回最长值

如果字符串包含列表中的任何项,JAVA将返回最长值,java,java-8,java-stream,Java,Java 8,Java Stream,我有以下代码类型数组: ["sample_code","code","formal_code"] 以及以下ID: String id="123456789_sample_code_xyz"; String id2="91343486_code_zxy"; 我想从ID中提取代码类型 这是我的代码片段: String codeTypes[] = {"sample_code","code","formal_code"}; String id= "123456789_sample_c

我有以下代码类型数组:

["sample_code","code","formal_code"]
以及以下ID:

String id="123456789_sample_code_xyz";
String id2="91343486_code_zxy";
我想从ID中提取代码类型

这是我的代码片段:

    String codeTypes[] = {"sample_code","code","formal_code"};
    String id= "123456789_sample_code_xyz";
    String codeType = Arrays.stream(codeTypes).parallel().filter(id::contains).findAny().get();
    System.out.println(codeType);
它不适用于第一个id,因为它返回“code”而不是“sample_code”,我想得到最长的代码类型

for the 1st id the code type should be "sample_code"
for the 2nd id the code type should be "code"

首先检查最长的代码类型。这意味着对代码进行以下更改:

  • 按长度降序排列代码类型
  • 不要使用平行流。平行流没有订单。顺序流确保按顺序检查代码类型
  • 使用
    findFirst()
    ,而不是
    findAny()
    ,以确保获得第一个匹配项
  • 因此,它变成:

        String codeTypes[] = { "sample_code", "code", "formal_code" };
        Arrays.sort(codeTypes, Comparator.comparing(String::length).reversed());
    
        String id = "123456789_sample_code_xyz";
        Optional<String> codeType = Arrays.stream(codeTypes).filter(id::contains).findFirst();
        codeType.ifPresent(System.out::println);
    
    字符串代码类型[]={“示例代码”、“代码”、“形式代码”};
    Arrays.sort(代码类型,Comparator.comparing(字符串::长度).reversed());
    String id=“123456789_示例_代码_xyz”;
    可选代码类型=Arrays.stream(代码类型).filter(id::contains).findFirst();
    代码类型.ifPresent(System.out::println);
    
    现在输出为:

    示例代码


    由于并行运行strem,因此无法预测哪个流首先找到匹配模式。在您的案例中(在我尝试您的代码片段时也是如此),第二个查找“代码”的流速度更快,整个流终止,因为您只需要“findAny()”


    删除“并行”后,您的代码将按预期工作。

    您可以按如下操作:

    public class Main {
        public static void main(String[] args) {
            String[] ids = { "123456789_sample_code_xyz", "91343486_code_zxy" };
            String[] codeTypes = { "sample_code", "code", "formal_code" };
            String max;
            for (String id : ids) {
                max = "";
                for (String codeType : codeTypes) {
                    if (id.contains(codeType)) {
                        if (max.length() < codeType.length()) {
                            max = codeType;
                        }
                    }
                }
                System.out.println(id + " : " + max);
            }
        }
    }
    

    嗯,流API并不是适用于所有问题。我认为您可以使用非流版本来解决您的问题

    我刚刚循环了
    codeTypes
    数组,对于每个代码类型,都用空字符串替换了
    idx
    ,然后计算其长度,找到
    idx
    字符串之间的最小长度,并进行替换。 现在,如果
    minSize
    length与
    replace
    字符串长度相同,那么它就是最终结果的候选<代码>最小尺寸!=id.length()用于不存在任何代码类型的时间

    private static String findCodeType(String id, String[] codeTypes) {
        int minSize = id.length();
        String codeType = "NotFound";
        for (String code : codeTypes) {
            String replace = id.replaceAll(code, "");
            minSize = Integer.min(minSize, replace.length());
            if (minSize == replace.length() && minSize != id.length())
                codeType = code;
        }
        return codeType;
    }
    

    你到底想要它归还什么
    private static String findCodeType(String id, String[] codeTypes) {
        int minSize = id.length();
        String codeType = "NotFound";
        for (String code : codeTypes) {
            String replace = id.replaceAll(code, "");
            minSize = Integer.min(minSize, replace.length());
            if (minSize == replace.length() && minSize != id.length())
                codeType = code;
        }
        return codeType;
    }