Jdk1.8 新機能 Stream ストリームには、anyMatch、allMatch、noneMatch の 3 つの API があり、それぞれの機能は次のとおりです。
List<String> list = Arrays.asList("a", "b", "c","d", ""); //任意一个字符串判断不为空则为true boolean anyMatch = list.stream().anyMatch( s->StringUtils.isEmpty(s)); //所有字符串判断都不为空则为true boolean allMatch = list.stream().allMatch( s->StringUtils.isEmpty(s)); //没有一个字符判断为空则为true boolean noneMatch = list.stream().noneMatch( s->StringUtils.isEmpty(s));
if(StringUtils.isEmpty(str1) || StringUtils.isEmpty(str2) || StringUtils.isEmpty(str3) || StringUtils.isEmpty(str4) || StringUtils.isEmpty(str5) || StringUtils.isEmpty(str6) ){ ..... }
if(Stream.of(str1, str2, str3, str4,str5,str6).anyMatch(s->StringUtils.isEmpty(s))){ ..... }
if(StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2) && StringUtils.isEmpty(str3) && StringUtils.isEmpty(str4) && StringUtils.isEmpty(str5) && StringUtils.isEmpty(str6) ){ ..... }
if(Stream.of(str1, str2, str3, str4,str5,str6).allMatch(s->StringUtils.isEmpty(s))){ ..... }
以上がJava で Stream を使用して if の多すぎる条件の判定を最適化する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。