Java - 将单个字符串值与 ArrayList 中的所有字符串值进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32201796/
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
Java - Comparing a single String value with all the String values in an ArrayList
提问by justcurious
I have an ArrayList
with a set of (same) string values which I need to compare with a single String value and return true or false. Is there any way to do
that in Java?
我有ArrayList
一组(相同的)字符串值,我需要将它们与单个字符串值进行比较并返回 true 或 false。有没有办法在Java中做到这一点?
For example, say I have a <String>ArrayList
with 5 values = foo, foo, foo, foo, foo (My requirement is such that all the values in the arraylist will be the SAME) and I have a String str = "foo"
. I need to verify that whether ALL the values in the arraylist is the SAME as the string value i.e., all the values present in the arraylist SHOULD be "foo"
.
例如,假设我有一个<String>ArrayList
5 个值 = foo, foo, foo, foo, foo (我的要求是数组列表中的所有值都相同)并且我有一个String str = "foo"
. 我需要验证 arraylist 中的所有值是否与字符串值相同,即 arraylist 中存在的所有值都应该是"foo"
。
I tried to google this info and all I can see is suggestions to use contains()
method, in different ways, which will return true even if anyone value in the arraylist contains the specified value.
我试图用谷歌搜索这个信息,我能看到的只是以contains()
不同方式使用方法的建议,即使数组列表中的任何值包含指定的值,它也会返回 true。
I even figured a workaround for this - Creating another arraylist with expected values and compare the two lists using equals()
method and it seems
to be working. I was just wondering whether there is any simple way to achieve this.
我什至想出了一个解决方法 - 创建另一个具有预期值的数组列表并使用equals()
方法比较两个列表,它似乎有效。我只是想知道是否有任何简单的方法可以实现这一目标。
回答by steffen
That's simple with Java 8:
Java 8 很简单:
String str = "foo";
List<String> strings = Arrays.asList("foo", "foo", "foo", "foo", "foo");
boolean allMatch = strings.stream().allMatch(s -> s.equals(str));
For Java 7 replace the last line with:
对于 Java 7,将最后一行替换为:
boolean allMatch = true;
for (String string : strings) {
if (!string.equals(str)) {
allMatch = false;
break;
}
}
回答by Amit Bhati
1) You can the pass the arraylist into a set.
1)您可以将arraylist传递到一个集合中。
2) Now you can get the size of set, if it is equal to 1 that means all elements are same.
2) 现在你可以得到集合的大小,如果它等于 1 表示所有元素都相同。
3) Now you can use the contains on set to check if your value is present in it or not.
3) 现在您可以使用集合中的 contains 来检查您的值是否存在于其中。
public static void main(String[] args){
String toBeCompared="foo";
List<String> list=new ArrayList<String>();
list.add("foo");
list.add("foo");
list.add("foo");
list.add("foo");
list.add("foo");
Set<String> set=new HashSet<String>(list);
if(1==set.size()){
System.out.println(set.contains(toBeCompared));
}
else{
System.out.println("List has different values");
}
}
回答by Jordi Castilla
If you want to know if the array contains the string use ArrayList::contains()
如果您想知道数组是否包含字符串,请使用ArrayList::contains()
String s = "HI";
ArrayList<String> strings = // here you have your string
if (string.contains(s)) {
// do your stuff
}
If you want to check if all valuesare same, iterate and count. If you have JAVA8 check steffen sollution.
如果要检查所有值是否相同,请迭代并计数。如果您有 JAVA8,请检查steffen 解决方案。
boolean areSame = true;
for (String str : strings) {
if (!str.equals(s)) areSame = false;
}
if (areSame) {
// all elements are same
}
回答by Kore
You can use this method to do that
你可以使用这个方法来做到这一点
private boolean allAreSame(ArrayList<String> stringList, String compareTo){
for(String s:stringList){
if(!s.equals(compareTo))
return false;
}
return true;
}
回答by JuergenKie
I would do it like this:
我会这样做:
ArrayList<String> foos = new ArrayList<>();
String str = "foo";
for (String string : foos) {
if(string.equals(str)){
System.out.println("True");
}
}