英文:
How to use startsWith and contains within a List of String
问题
我有这个实现,我需要检查基于接受的版本列表,以确定是否接受具有x.x.x格式的版本。例如,如果1.3在接受的版本列表中,那么1.3.1、1.3.2或简单地1.3.x都是被接受的。如果1.2不在列表中,那么1.2.x就不被接受。
@Test
public test() {
Assert.assertTrue(isVersionAccepted("1.3.2"));
Assert.assertFalse(isVersionAccepted("1.2.1"));
}
public static boolean isVersionAccepted(String version) {
List<String> acceptedVersions = Arrays.asList("1.1", "1.3", "1.5", "2.5", "2.7", "3.1", "3.2");
// 处理
}
英文:
i have this implementation that I need to check whether the version with x.x.x format is accepted based on accepted version in the list with just a format of x.x.
For example, if 1.3 is in the accepted version list. then 1.3.1, 1.3.2 or simply 1.3.x is accepted. As if 1.2 is not in the list then 1.2.x is not accepted.
@Test
public test() {
Assert.assertTrue(isVersionAccepted("1.3.2"));
Assert.assertFalse(isVersionAccepted("1.2.1"));
}
public static boolean isVersionAccepted(String version) {
List<String> acceptedVersions = Arrays.asList("1.1", "1.3", "1.5", "2.5", "2.7", "3.1", "3.2");
// process
}
答案1
得分: 0
你可以使用 Stream#anyMatch
来检查输入是否以 List
中的任何值开头。
return acceptedVersions.stream().anyMatch(version::startsWith);
英文:
You can use Stream#anyMatch
to check if the input starts with any of the values in the List
.
return acceptedVersions.stream().anyMatch(version::startsWith);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论