How do I determine whether an array contains a particular value in Java?

后端 未结 29 2509
予麋鹿
予麋鹿 2020-11-21 05:00

I have a String[] with values like so:

public static final String[] VALUES = new String[] {\"AB\",\"BC\",\"CD\",\"AE\"};

Given

相关标签:
29条回答
  • 2020-11-21 05:30

    Just simply implement it by hand:

    public static <T> boolean contains(final T[] array, final T v) {
        for (final T e : array)
            if (e == v || v != null && v.equals(e))
                return true;
    
        return false;
    }
    

    Improvement:

    The v != null condition is constant inside the method. It always evaluates to the same Boolean value during the method call. So if the input array is big, it is more efficient to evaluate this condition only once, and we can use a simplified/faster condition inside the for loop based on the result. The improved contains() method:

    public static <T> boolean contains2(final T[] array, final T v) {
        if (v == null) {
            for (final T e : array)
                if (e == null)
                    return true;
        } 
        else {
            for (final T e : array)
                if (e == v || v.equals(e))
                    return true;
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-11-21 05:31

    ObStupidAnswer (but I think there's a lesson in here somewhere):

    enum Values {
        AB, BC, CD, AE
    }
    
    try {
        Values.valueOf(s);
        return true;
    } catch (IllegalArgumentException exc) {
        return false;
    }
    
    0 讨论(0)
  • 2020-11-21 05:31

    Arrays.asList() -> then calling the contains() method will always work, but a search algorithm is much better since you don't need to create a lightweight list wrapper around the array, which is what Arrays.asList() does.

    public boolean findString(String[] strings, String desired){
       for (String str : strings){
           if (desired.equals(str)) {
               return true;
           }
       }
       return false; //if we get here… there is no desired String, return false.
    }
    
    0 讨论(0)
  • 2020-11-21 05:32

    If you don't want it to be case sensitive

    Arrays.stream(VALUES).anyMatch(s::equalsIgnoreCase);
    
    0 讨论(0)
  • 2020-11-21 05:33

    If the array is not sorted, you will have to iterate over everything and make a call to equals on each.

    If the array is sorted, you can do a binary search, there's one in the Arrays class.

    Generally speaking, if you are going to do a lot of membership checks, you may want to store everything in a Set, not in an array.

    0 讨论(0)
  • 2020-11-21 05:33

    With Java 8 you can create a stream and check if any entries in the stream matches "s":

    String[] values = {"AB","BC","CD","AE"};
    boolean sInArray = Arrays.stream(values).anyMatch("s"::equals);
    

    Or as a generic method:

    public static <T> boolean arrayContains(T[] array, T value) {
        return Arrays.stream(array).anyMatch(value::equals);
    }
    
    0 讨论(0)
提交回复
热议问题