在 Java 中检查 HashSet 是否包含某个值
假如我们的整型数组如下所示 -
Integer arr[] = { 50, 100, 150, 200, 250, 300 };在 HashSet 中设置以上整型数组-
Set<Integer>set = new HashSet<Integer>(Arrays.asList(arr));
现在,让我们检查 HashSet 是否包含某个值 -
set.contains(150)
如果值在列表中,则返回 TRUE,否则返回 FALSE。
示例
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Demo {
public static void main(String[] a) {
Integer arr[] = { 50, 100, 150, 200, 250, 300 };
Set<Integer>set = new HashSet<Integer>(Arrays.asList(arr));
System.out.println(set.contains(200));
System.out.println(set.contains(150));
System.out.println(set.contains(100));
System.out.println(set.contains(10));
}
}输出
true true true false
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP