HashSet基本使用

 大家在做下面练习时,重点体会“Set是无序、不可重复”的核心要点。

【示例】HashSet的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Test {
    public static void main(String[] args) {
        Set<String> s = new HashSet<String>();
        s.add("hello");
        s.add("world");
        System.out.println(s);
        s.add("hello"); //相同的元素不会被加入
        System.out.println(s);
        s.add(null);
        System.out.println(s);
        s.add(null);
        System.out.println(s);
    }
}

      执行结果如图所示:

图9-24示例9-9运行效果图.png

原文地址:https://www.cnblogs.com/huaxiansheng/p/15317780.html