28681

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

Question:

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?

Answer1:

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");

Answer2:

This is a standard part of the Java Collections API:

boolean exists = listObject.contains(str);

Recommend

  • Default Credentials in Google App Engine: Invalid Credentials error
  • How to convert java list of objects to 2D array?
  • Spring Data JPA (CrudRepository) - BeanCreationException: Could not autowire field
  • What is the time complexity of Python list's count() function?
  • Requested JSON parse failed
  • Task Scheduler does not run Excel VBA Code to send PDF as Email Attachment
  • Can an Excel VBA UDF called from the worksheet ever be passed an instance of any Excel VBA object mo
  • How to update data into a file in a particular position in js
  • Why is this code not working? Hangman
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • Open an application in a space using applescripts
  • how to populate a SQLite database and use that database in phonegap?
  • How to split circle in to the sectors in google maps?
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • How to override value that appears in a dropdown in the rails_admin gem
  • UIAlertController button function not working
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • How to modify the colors in the legend of a plot using a fill gradient?
  • How to handle AllServersUnavailable Exception
  • Google cloud sdk not working when python points python3
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal