在 Java 中,检查字符串是否在列表中的最优雅/惯用的方法是什么?
例如,如果我有 String searchString = "abc"; List<String> myList = ....
, 什么是 Java 等同于我在 Perl 中做的事情:
my $isStringInList = grep { /^$searchString$/ } @myList;
# or in Perl 5.10+
my $isStringInList = $searchString ~~ @myList;
?
最佳答案
您可以使用列表 contains检查字符串是否存在于列表中的方法:
公共(public) boolean 值包含(对象 o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
只需像这样使用它:
myList.contains(searchString);
关于java - 检查字符串是否在列表中的最优雅/惯用的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19252857/