检查Java LinkedHashSet中是否存在特定元素
使用contains()方法检查LinkedHashSet中是否存在特定元素。
我们首先创建一个LinkedHashSet并添加一些元素-
LinkedHashSet<String> l = new LinkedHashSet<String>();
l.add(new String("1"));
l.add(new String("2"));
l.add(new String("3"));
l.add(new String("4"));
l.add(new String("5"));
l.add(new String("6"));
l.add(new String("7"));现在,检查它是否包含元素“5”-
l.contains("5")以下是检查LinkedHashSet中是否存在特定元素的示例-
示例
import java.util.*;
public class Demo {
public static void main(String[] args) {
LinkedHashSet<String> l = new LinkedHashSet<String>();
l.add(new String("1"));
l.add(new String("2"));
l.add(new String("3"));
l.add(new String("4"));
l.add(new String("5"));
l.add(new String("6"));
l.add(new String("7"));
System.out.println("LinkedHashSet elements...");
System.out.println(l);
System.out.println("Does 5 exist in the LinkedHashSet elements? "+l.contains("5"));
}
}输出结果
LinkedHashSet elements... [1, 2, 3, 4, 5, 6, 7] Does 5 exist in the LinkedHashSet elements? True
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短