简体   繁体   English

检查集合是否包含与某个特定值匹配的所有值

[英]Check a collection if it contains all the values that match a certain value

I want to know what the best way to know if all values of a collection match a particular value. 我想知道哪种最佳方法可以知道某个集合的所有值是否都与某个特定值匹配。

List<Integer> listOfStrings = new ArrayList<Integer>();

All I want to know is if all the entries in the "listOfStrings" match a particular value; 我只想知道“ listOfStrings”中的所有条目是否都与一个特定值匹配; for instance there is no integer that is not '1'. 例如,没有不为“ 1”的整数。

I need the fastest solution to this. 我需要最快的解决方案。 I have a solution with me but it is very rudimentary. 我有一个解决方案,但是非常简单。

Loop through it: 循环遍历:

public boolean checkAll(ArrayList<String> list) {
    for(int i = 0; i < listOfStrings.size(); i++) {
        String candidate = listOfStrings.get(i);
        if(candidate == null || !candidate.equals("1")) {
            return false;
        }
    }
    return true;
}

You could just iterate over your list with a simple for loop, and compare every value with your certain value (say 1). 您可以使用简单的for循环遍历列表,然后将每个值与您的特定值进行比较(例如1)。 If one value from your list is not equal to the certain value, just set a boolean to false. 如果列表中的一个值不等于特定值,则将布尔值设置为false。

Just do this 做这个

public boolean isFilled(String value, ArrayList<String> list)
{
    for(int i = 0; i < list.size(); i++)
    {
        String toTest = list.get(i);
        if(toTest == null || !toTest.equals(value)) {
            return false;
        }
    }

    return true;
}

In Java 8 you can use the allMatch method of a stream to achieve this. 在Java 8中,可以使用流的allMatch方法来实现此目的。

public boolean allOnes(Collection<Integer> values) {
    return values.parallelStream().allMatch(i -> i == 1);
}

Using a parallelStream will potentially yield better performance if there are multiple processors available. 如果有多个处理器,使用parallelStream可能会产生更好的性能。

 boolean x=true;
     ArrayList<Integer> listOfInts = new ArrayList<Integer>   (Arrays.asList(5,3,1,2,9,5,0,7));
    Integer target = 1;
    for (int i = 0; i < listOfInts.size(); i++)
    {
        if (listOfInts.get(i).equals(target)) // nothing
        {

        } else {
            x = false;
            break;// exits loop right after this
        }
    }
    System.out.println(x);

To my astonishment, all the solutions proposed so far are just plain wrong, or contain a subtle bug, or could be more efficient. 令我惊讶的是,到目前为止提出的所有解决方案都是错误的,或者包含一个细微的错误,或者可能更有效。

Here's a solution that works fast whatever the kind of list is, and even if the list contains null elements: 无论列表类型如何,即使列表包含null元素,此解决方案都可以快速运行:

public boolean listIsFilledWith(List<Integer> integers, int i) {
    Integer value = i; // transform the int into an Integer only once
    for (Integer element : integers) { // iterate using an iterator, to avoid performance problems with linked lists
        if (!value.equals(element)) { // value is not null, but element can be. Don't compare objects with == or !=
            return false; // return early
        }
    }
    return true;
}

Use the .contains() method that tells you whether the value exists in the list : http://docs.oracle.com/javase/1.4.2/docs/api/java/util/List.html#contains%28java.lang.Object%29 使用.contains()方法,该方法告诉您列表中是否存在该值: http : .contains() 。 lang.Object 29%

myList.contains(1);

Note: it uses the .equals() method of the contained object (it might be useful in your future developments). 注意:它使用所包含对象的.equals()方法(在将来的开发中可能会很有用)。

HIH M. HIH M.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查子字符串是否包含某个值 - Check if a substring contains a certain value 有没有办法检查 Stream 是否包含所有集合元素? - Is there a way to check if a Stream contains all collection elements? 使用 JPA Criteria API 返回集合属性包含特定值的所有对象 - Using JPA Criteria API to return all object where a Collection property contains a certain value 如何使用 spring 引导获取 mongodb 中包含特定(字符串)值的所有集合键 - How to get all keys of collection that contains certain(String) value in mongodb using spring boot 如何检查数组中的所有值是否都具有特定值? - How can I check if all values in an array have a certain value? 检查JSON响应是否包含特定值 - Check if JSON response contains certain value 如何检查arraylist中的数组是否包含某个值? - How to check if an array in an arraylist contains a certain value? "检查springboot模型列表是否包含某个值" - Check if springboot model list contains a certain value 如何检查字符串列表是否包含特定值? - How to check if a list of strings contains certain value? 如何检查数据库中的行是否包含特定值 - how to check if a row in a database contains a certain value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM