检查 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP