如何查找元素是否存在于列表中或不使用Google Guava? [英] How can I find an element whether it exists in a List or not using Google Guava?

查看:1045
本文介绍了如何查找元素是否存在于列表中或不使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆