Gnik
Gnik

Reputation: 7426

How can I find an element whether it exists in a List or not using Google Guava?

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

Answers (2)

yamilmedina
yamilmedina

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

Stewart
Stewart

Reputation: 18304

This is a standard part of the Java Collections API:

boolean exists = listObject.contains(str);

Upvotes: 18

Related Questions