HashSet集合无索引 不可以重复 无序

来源:互联网 发布:死亡笔记电影知乎 编辑:程序博客网 时间:2024/06/10 15:11
//set集合无索引 不可以重复  无序
        
        //demo1();
        HashSet<Person> hs =new HashSet<>();
        hs.add(new Person("张三",23));
        hs.add(new Person("李四",24));
        hs.add(new Person("张三",23));
        hs.add(new Person("李四",24));
        hs.add(new Person("李四",24));
        hs.add(new Person("李四",24));
        System.out.println(hs.size());
    }

    public static void demo1() {
        HashSet<String> hs =new HashSet<>();
    boolean  b1=    hs.add("a");
    boolean  b2= hs.add("a");//当向set集合中存储重复元素返回false
    b1=    hs.add("d");
    b1=    hs.add("c");
    b1=    hs.add("e");
    b1=    hs.add("f");
    
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(hs);//hashset的继承体系中有tostring方法
        for (String string : hs) {    //只要能用迭代器迭代的 就可以用增强for循环遍历
            System.out.println(string);
            
        }
    }
原创粉丝点击