30 HashSet

/*
* 使用HashSet存储字符串并遍历
*
* Set的特点:
* 无序(存储和读取的顺序可能不一样)
* 不允许重复
* 没有整数索引

于List正好相反

*/

public class HashSetDemo {
    public static void main(String[] args) {
        //创建集合对象
        HashSet<String> hs = new HashSet<String>();

        //添加元素
        hs.add("hello");
        hs.add("world");
        hs.add("java");
//        hs.add("java");

        //遍历集合对象
        for (String s: hs) {
            System.out.println(s);
        }
    }
}

输出

原文地址:https://www.cnblogs.com/longesang/p/11268998.html