Java 数组搜索 [英] Java Array Searching
问题描述
在java中一个数组有三个词{'bad','bat','hat'}
.我必须编写一个函数,该函数将像字典一样从数组中搜索.
In java an array has three words {'bad','bat','hat'}
. I have to write a function which will search like dictionary from array.
示例:如果我搜索 at,那么该函数将返回所有包含 at 的单词(例如 bat
和 hat
将显示在输出中),如果我搜索单个字符字符,那么它将返回所有具有该字符的单词.Java 中是否有可用的内置函数?
Example: If I search at then the function will return all the words which have at in it (like bat
and hat
will be shown in the output) and if I search a single character character then it will return all those words which have that character. Is there any built-in function available in Java?
例如:if (string.contains(item1) || string.contains(item2) || string.contains(item3))
推荐答案
您可以流式传输数组,并使用 String::contains
.
You could stream the array, and filter using String::contains
.
String[] words = {"bad", "bat","hat"};
String filter = "at";
List<String> list = Stream.of(words)
.filter(word -> word.contains(filter))
.collect(Collectors.toList());
System.out.println(list);
这篇关于Java 数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!