如何查找元素是否存在于列表中或不使用Google Guava? [英] How can I find an element whether it exists in a List or not using Google Guava?
问题描述
我有一个 List< String>
我需要找到一个列表中是否存在特定的字符串。
例如:
String str =apple;
List< String> listObject = Lists.newArrayList(apple,orange,banana);
我需要找到
str
存在于 listObject
或不使用Google Guava。 所以我需要一个 true
或 false
结果。
如何实现这一点?
这可以使用标准的集合API(也应该),但无论如何,在Guava你可以这样做:
List< String> strList = Arrays.asList(new String [] {one,two,3,4});
boolean exists = FluentIterable.from(strList).contains(two);
I have a List<String>
I need to find whether the particular string exists inside a List or not.
for Eg:
String str = "apple";
List<String> listObject = Lists.newArrayList("apple", "orange", "banana");
I need to find whether str
exists in listObject
or not using Google Guava.
So I need a true
or false
result.
How can I achieve this?
I'm agree that this can be done (and should, maybe) with the standard Collections API, but anyway, in Guava you can do it like this:
List<String> strList = Arrays.asList(new String[] {"one", "two", "3", "4"});
boolean exists = FluentIterable.from(strList).contains("two");
这篇关于如何查找元素是否存在于列表中或不使用Google Guava?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!