Java我有一个字符串a我也有一个字符串的List如何判断List里的每个字符串是否是这个字符串a的开头使用stream
可以使用Java 8的Stream API来判断List中每个字符串是否是给定字符串a的开头。下面是一个示例代码:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String a = "example";
List<String> strings = Arrays.asList("example1", "example2", "test");
// 使用Stream判断List中每个字符串是否是给定字符串a的开头
boolean allStartsWithA = strings.stream()
.allMatch(s -> s.startsWith(a));
System.out.println("Are all strings starting with 'example'? " + allStartsWithA);
}
}
在上面的示例中,我们定义了一个字符串a和一个字符串列表strings。然后,我们使用Stream的allMatch()方法来判断列表中的每个字符串是否都是以给定字符串a开头。最后,我们打印出判断结果。
运行上面的代码,输出结果为:
Are all strings starting with 'example'? false
根据列表中的字符串,我们可以看到并不是所有的字符串都以"example"开头。
原文地址: https://www.cveoy.top/t/topic/i2n8 著作权归作者所有。请勿转载和采集!