Reputation: 7426
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?
Upvotes: 1
Views: 2673
Reputation: 3395
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");
Upvotes: 4
Reputation: 18304
This is a standard part of the Java Collections API:
boolean exists = listObject.contains(str);
Upvotes: 18