Java – Check if Array contains a certain value?

Java – Check if Array contains a certain value?
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

from:http://www.mkyong.com/java/java-check-if-array-contains-a-certain-value/

原文地址:https://www.cnblogs.com/shy1766IT/p/10105113.html

时间: 2024-10-13 00:36:08

Java – Check if Array contains a certain value?的相关文章

Check whether array A is a permutation.

Task description A non-empty zero-indexed array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2 is a permuta

Java之数据array和集合list、set、map

之前一直分不清楚java中的array,list.同时对set,map,list的用法彻底迷糊,直到看到了这篇文章,讲解的很清楚. 世间上本来没有集合,(只有数组参考C语言)但有人想要,所以有了集合 有人想有可以自动扩展的数组,所以有了List 有的人想有没有重复的数组,所以有了set 有人想有自动排序的组数,所以有了TreeSet,TreeList,Tree** 而几乎有有的集合都是基于数组来实现的. 因为集合是对数组做的封装,所以,数组永远比任何一个集合要快 但任何一个集合,比数组提供的功能

Java parser Json Array to List&amp;lt;T&amp;gt;

摘要:Java parser Json Array to List use Gson . commos.io ? ? ? ? ? ? ? ? ? ? ? ? ? ?String jsonArray = FileUtils.readFileToString(json_file,"UTF-8"); ? ? ? ? ? ? ? ? ? ? ? ? ? ?Gson gson = new Gson(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?Type listType = new

Java中对Array数组的常用操作

目录: 声明数组: 初始化数组: 查看数组长度: 遍历数组: int数组转成string数组: 从array中创建arraylist: 数组中是否包含某一个值: 将数组转成set集合: 将数组转成list集合: Arrays.fill()填充数组: 数组排序: 复制数组: 比较两个数组: 去重复: 查询数组中的最大值和最小值: 备注:文内代码具有关联性. 1.声明数组: String [] arr; int arr1[]; String[] array=new String[5]; int sc

java数组和Array类

java数组英文:Arrays 存储相同数值的集合的数据结构 An array is a data structure that stores a collection of values of the same type. You accesseach individual value through an integer index. For example, if a is an array of integers, thena[i] is the ith integer in the a

Java 中的Array工具类

1.boolean equals(array1,array2):比较两个数组是否相等.import java.util.Arrays;public class Ch03 { public static void main(String[] args) { // TODO Auto-generated method stub String[] str1={"1","2","3"}; String[] str2={"1",&quo

java List与array互相转换

叔叔不说  不解释 不约,直接上代码 package cn.kge.one; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo1 { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("a"

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

Java基础——数组Array

一.数组基本概念 数组是相同类型数据的有序集合. 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成.其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们. 数组有三个特点: 1. 其长度是确定的.数组一旦被创建,它的大小就是不可以改变的.不可越界,如果越界,则报:ArrayIndexOutOfBoundsException 2. 其元素必须是相同类型,不允许出现混合类型. 3. 数组中的元素可以是任何数据类型,包括基本类型和引用类型. 数组不属于八种基本数据类