Java – Check if Array contains a certain value? 修改教程

Java examples to check if an Array (String or Primitive type) contains a certain values, updated with Java 8 stream APIs.

1. String Arrays

1.1 Check if a String Array contains a certain value “A”.

StringArrayExample1.java

package com.mkyong.core;

import java.util.Arrays;
import java.util.List;

public class StringArrayExample1 {

    public static void main(String[] args) {

        String[] alphabet = new String[]{"A", "B", "C"};

        // Convert String Array to List
        List<String> list = Arrays.asList(alphabet);

        if(list.contains("A")){
            System.out.println("Hello A");
        }

    }

}

Output

Hello A

In Java 8, you can do this :

// Convert to stream and test it
    boolean result = Arrays.stream(alphabet).anyMatch("A"::equals);
    if (result) {
        System.out.println("Hello A");
    }

1.2 Example to check if a String Array contains multiple values :

StringArrayExample2.java

package com.mkyong.core;

import java.util.Arrays;
import java.util.List;

public class StringArrayExample2 {

    public static void main(String[] args) {

        String[] alphabet = new String[]{"A", "C"};

        // Convert String Array to List
        List<String> list = Arrays.asList(alphabet);

        // A or B
        if (list.contains("A") || list.contains("B")) {
            System.out.println("Hello A or B");
        }

        // A and B
        if (list.containsAll(Arrays.asList("A", "B"))) {
            System.out.println("Hello A and B");
        }

        // A and C
        if (list.containsAll(Arrays.asList("A", "C"))) {
            System.out.println("Hello A and C");
        }

    }

}

Output

Hello A or B
Hello A and C

2. Primitive Arrays

2.1 For primitive array like int[], you need to loop it and test the condition manually :

PrimitiveArrayExample1.java

package com.mkyong.core;

import java.util.Arrays;
import java.util.List;

public class PrimitiveArrayExample1 {

    public static void main(String[] args) {

        int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        if(contains(number, 2)){
            System.out.println("Hello 2");
        }

    }

    public static boolean contains(final int[] array, final int v) {

        boolean result = false;

        for(int i : array){
            if(i == v){
                result = true;
                break;
            }
        }

        return result;
    }

}

Output

Hello 2

2.2 With Java 8, coding is much simpler ~

ArrayExample1.java

package com.mkyong.core;

import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class TestDate {

    public static void main(String[] args) {

        int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        //Java 8
        boolean result = IntStream.of(number).anyMatch(x -> x == 4);

        if (result) {
            System.out.println("Hello 4");
        } else {
            System.out.println("Where is number 4?");
        }

        long[] lNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        boolean result2 = LongStream.of(lNumber).anyMatch(x -> x == 10);

        if (result2) {
            System.out.println("Hello 10");
        } else {
            System.out.println("Where is number 10?");
        }

    }

}

Output

Hello 4
Hello 10

Note
To check if a primitive array contains multiple values, convert the array into a List and compare it like example 1.2 above.

References

  1. IntStream JavaDoc
  2. Arrays.asList JavaDoc

如果觉得JSRUN的教程对您有用,请随意打赏。您的支持将鼓励JSRUN继续提供更好的服务

捐助JSRUN
JSRUN前端教程共编系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的本前端教程,让知识的积累变得统一完整、自成体系。 希望广大工程师能一起参与进共编,让零散在世界各处的知识点找到组织、找到家。
X
支付宝
9.99
请使用支付宝扫码支付, 有效时间 5分0秒
超过有效时间后请立即停止支付。
支付故障请联系QQ:565830900
因10.00元通道已占用,系统建议您支付9.99元
正在生成二维码, 此过程可能需要15秒钟
谢谢支持,我一定会更加努力的