Java程序检查HashSet中是否存在特定元素
使用contains()方法检查特定元素是否存在-
Set<Integer> hs = new HashSet<Integer>(); hs.add(30); hs.add(67); hs.add(88); hs.add(33); hs.add(54); hs.add(90); hs.add(66); hs.add(79);
要检查元素89,使用contains()方法-
hs.contains(89));
以下是检查HashSet中是否存在特定元素的示例-
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
//创建哈希集
Set<Integer> hs = new HashSet<Integer>();
hs.add(30);
hs.add(67);
hs.add(88);
hs.add(33);
hs.add(54);
hs.add(90);
hs.add(66);
hs.add(79);
System.out.println("Elements in set = "+hs);
System.out.println("Does 89 in the set? "+hs.contains(89));
System.out.println("Does 67 in the set? "+hs.contains(67));
}
}输出结果
Elements in set = [33, 66, 67, 54, 88, 90, 30, 79] Does 89 in the set? false Does 67 in the set? True
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短