0
点赞
收藏
分享

微信扫一扫

HashSet常用方法

夕阳孤草 2022-04-20 阅读 96
java

一、介绍

HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。

HashSet 允许有 null 值。

HashSet 是无序的,即不会记录插入的顺序。

HashSet 不是线程安全的, 如果多个线程尝试同时修改 HashSet,则最终结果是不确定的。 您必须在多线程访问时显式同步对 HashSet 的并发访问。

HashSet 实现了 Set 接口。

二、添加元素

方法:add()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    set.add("1");  // 重复的元素不会被添加
    set.add("2");  // 重复的元素不会被添加
    System.out.println(set);
}

运行结果:

[1, 2, 3, 4]

三、判断元素是否存在

方法:contains()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    boolean contains1 = set.contains("1");
    boolean contains2 = set.contains("5");
    System.out.println(contains1);
    System.out.println(contains2);
}

运行结果:

true
false

四、删除元素

方法:remove()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    System.out.println("删除前-----"+set);
    set.remove("1");
    System.out.println("删除后-----"+set);
}

运行结果:

删除前-----[1, 2, 3, 4]
删除后-----[2, 3, 4]

五、删除集合中所有元素

方法:clear()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    System.out.println("删除前-----"+set);
    set.clear();
    System.out.println("删除后-----"+set);
}

运行结果:

删除前-----[1, 2, 3, 4]
删除后-----[]

六、计算大小

方法:size()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    int size = set.size();
    System.out.println(size);
}

运行结果:

4

七、迭代 HashSet

方法:for()    foreach()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    for (String s : set) {
        System.out.println("普通for循环"+s);
    }
    set.forEach(r->{
        System.out.println("foreach遍历"+r);
    });
}

运行结果:

普通for循环1
普通for循环2
普通for循环3
普通for循环4
foreach遍历1
foreach遍历2
foreach遍历3
foreach遍历4

八、判断是否为空

方法:isEmpty()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    HashSet<String> set1 = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    boolean empty1 = set.isEmpty();
    boolean empty2 = set1.isEmpty();
    System.out.println(empty1);
    System.out.println(empty2);
}

运行结果:

false
true

九、复制原列表

方法:clone()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    System.out.println("原列表---"+set);
    Object clone = set.clone();
    System.out.println("复制列表---"+clone);
}

运行结果:

原列表---[1, 2, 3, 4]
复制列表---[1, 2, 3, 4]

十、转换为字符串

方法:toString()

代码:

public static void main(String[] args) {
    HashSet<String> set = new HashSet<>();
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("4");
    System.out.println("原列表---"+set);
    String s = set.toString();
    System.out.println("转换成字符串---"+s);
}

运行结果:

原列表---[1, 2, 3, 4]
转换成字符串---[1, 2, 3, 4]

举报

相关推荐

0 条评论