如何检查 arrayList 是否具有值内部的值
How to check if an arrayList has a value inside of a value
如何检查 ArrayList 中的值中是否包含一组特定的字母?
ArrayList<String> words = new ArrayList<String>();
words.add("Wooden Axe");
words.add("Stone Axe");
if(words.contains("Axe")) {
//something
}
比如如何检查值是否包含字符串“Axe”?
您可以使用 Stream 方法 allMatch();
if(words.stream().allMatch(word -> word.contains("Axe"))) {
//something
}
如果你想在之后做一个处理,你可以这样做:
words.stream()
.filter(word -> word.contains("Axe")) // you only keep words with "Axe"
.forEach(word -> System.out.println(word)); // you process them the way you want
对于更复杂的答案,您需要像这样遍历数组列表本身中的每个项目:
public static boolean ArrayContainsWord(String match){
for(String word : words){
if(word.Contains(match)){
return true;
}
}
return false;
}
如果您想找到与您想要的单词匹配的确切单词,那么您可以将方法“ArrayContainsWord”的 return 类型更改为 return a String
而不是 boolean
和 return true
行中的 return 单词和 return null
当您找不到任何单词时。
如何检查 ArrayList 中的值中是否包含一组特定的字母?
ArrayList<String> words = new ArrayList<String>();
words.add("Wooden Axe");
words.add("Stone Axe");
if(words.contains("Axe")) {
//something
}
比如如何检查值是否包含字符串“Axe”?
您可以使用 Stream 方法 allMatch();
if(words.stream().allMatch(word -> word.contains("Axe"))) {
//something
}
如果你想在之后做一个处理,你可以这样做:
words.stream()
.filter(word -> word.contains("Axe")) // you only keep words with "Axe"
.forEach(word -> System.out.println(word)); // you process them the way you want
对于更复杂的答案,您需要像这样遍历数组列表本身中的每个项目:
public static boolean ArrayContainsWord(String match){
for(String word : words){
if(word.Contains(match)){
return true;
}
}
return false;
}
如果您想找到与您想要的单词匹配的确切单词,那么您可以将方法“ArrayContainsWord”的 return 类型更改为 return a String
而不是 boolean
和 return true
行中的 return 单词和 return null
当您找不到任何单词时。