如何将 String 值与 Java 中 String 类型的 ArrayList 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6607888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I compare String value with ArrayList of String type in Java?
提问by shiva
My requirement is comparing String
to ArrayList
which contains a list of strings. Can any one suggest me?
我的要求是比较String
到ArrayList
包含字符串列表。任何人都可以建议我吗?
回答by Bohemian
Look at the List#contains(T obj)method, like this:
看一下List#contains(T obj)方法,像这样:
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("xyz");
list.contains("abc"); // true
list.contains("foo"); // false
回答by Logan
Use
用
ArrayList.contains("StringToBeChecked");
If the String is present in the ArrayList, this function will return true, else will return false.
如果字符串存在于 ArrayList 中,则此函数将返回 true,否则将返回 false。
回答by Java_Waldi
This is your method:
这是你的方法:
private boolean containsString(String testString, ArrayList<String> list)
{
return list.contains(testString);
}
回答by Snicolas
There are different options you could consider :
您可以考虑不同的选择:
- a simple for each loop using equals on each member of your list to compare it to string A.
- sorting all strings in list in order to boost up comparison and find if a string in the list matches A.
- using a sortedSet to achieve the above
- putting alls string in the intern string pool using intern method on every item in the list and on string A. This will allow to compare strings using == and not equal anymore. Comparison would then be much faster.
- using a hashmap to achieve better comparison speeds
- an identity hashmap to mix the two preceding strategies.
- 一个简单的每个循环,在列表的每个成员上使用 equals 将其与字符串 A 进行比较。
- 排序列表中的所有字符串以增强比较并查找列表中的字符串是否与 A 匹配。
- 使用 sortedSet 来实现上述目的
- 在列表中的每个项目和字符串 A 上使用 intern 方法将所有字符串放入实习字符串池中。这将允许使用 == 比较字符串,而不是不再相等。然后比较会快得多。
- 使用哈希图来获得更好的比较速度
- 混合上述两种策略的身份哈希映射。
Well, the are pros and cons to those methods, it all depends on what you want to do and why and how you compare strings.
好吧,这些方法各有利弊,这完全取决于您想要做什么以及为什么以及如何比较字符串。
Regards, Stéphane
问候, 斯蒂芬
回答by Tapas Bose
If you are willing to use Lambdaj, then check the presence of the String as:
如果您愿意使用Lambdaj,请检查 String 是否存在:
private boolean isContains(ArrayList<String> listOfStrings, String testString) {
return (Lambda.select(listOfStrings, Matchers.equalTo(testString)).size() != 0);
}
Using static import of select
and equalTo
increase the readability:
使用静态导入select
并equalTo
增加可读性:
select(listOfStrings, equalTo(testString));
回答by Tapas Bose
ArrayList can contain one or more Strings. You cannot compare String with ArrayList. But you can check whether the ArrayList contains that String ot not using contains()
method
ArrayList 可以包含一个或多个字符串。您不能将 String 与 ArrayList 进行比较。但是您可以检查 ArrayList 是否包含该 String ot not using contains()
method
String str = //the string which you want to compare
ArrayList myArray =// my array list
boolean isStringExists = myArray.contains(str);// returns true if the array list contains string value as specified by user