Java基础-Set

1、HashSet

public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet<String> hs = new HashSet<String>();
        hs.add("12");
        hs.add("13");
        hs.add("14");
        hs.add("15");
        hs.add("16");
        hs.add("14");
    
        Iterator<String> it =hs.iterator();
        while(it.hasNext()){
            String s =it.next();
            System.out.println(s);
        }

2、LinkedHashSet是Set集合的一个实现,具有set集合不重复的特点,同时具有可预测的迭代顺序,也就是我们插入的顺序。并且linkedHashSet是一个非线程安全的集合。如果有多个线程同时访问当前linkedhashset集合容器,并且有一个线程对当前容器中的元素做了修改,那么必须要在外部实现同步保证数据的冥等性.

3、TreeSet

1) TreeSet是SortedSet接口的唯一实现, TreeSet可以确保集合元素处于排序状态.  TreeSet并不是根据元素的插入顺序进行排序,

        而是根据元素实际值来进行排序.(可以确保元素唯一并且元素排序)  TreeSet采用红黑树的数据结构对元素进行排序.
2)TreeSet支持两种排序方法:  自然排序和比较器排序.  默认情况下,TreeSet 采用自然排序.(Integer类型元素自然升序)

4、HashMap

HashMap 是基于哈希表的 Map 接口的非同步实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。
此类不保证映射的顺序,特别是它不保证该顺序恒久不变。

HashMap():构建一个初始容量为 16,负载因子为 0.75 的 HashMap。
ashMap(int initialCapacity):构建一个初始容量为 initialCapacity,负载因子为 0.75 的 HashMap。
HashMap(int initialCapacity, float loadFactor):以指定初始容量、指定的负载因子创建一个 HashMap。

        HashMap<String,Student> hm = new HashMap<String,Student>();
        hm.put("1hao", new Student("zhangsan",19));
        hm.put("2hao", new Student("zhangsi",20));
        hm.put("3hao", new Student("zhangwu",21));
        hm.put("4hao", new Student("zhangliu",22));
        
        System.out.print(hm.get("1hao").getName());

1)keySet()获取key集合,再获取迭代器

 */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap<String,Student> hm = new HashMap<String,Student>();
        hm.put("1hao", new Student("zhangsan",19));
        hm.put("2hao", new Student("zhangsi",20));
        hm.put("3hao", new Student("zhangwu",21));
        hm.put("4hao", new Student("zhangliu",22));
        
        System.out.print(hm.get("1hao").getName());
        
        Iterator<String> it = hm.keySet().iterator();//获取key集合
        while(it.hasNext()){
            String key = it.next();//获取key
            Student st = hm.get(key);//获取key对应的value
            System.out.println(st.getName());
        }
    }

5、Iterator和foreach遍历集合

LinkedList<Student> ll = new LinkedList<Student>();
        ll.add(new Student("zhangsan",13));
        ll.add(new Student("zhangsi",14));
        ll.add(new Student("zhangwu",15));
        ll.add(new Student("zhangliu",16));
        ll.add(new Student("zhangqi",17));
        
        Iterator<Student> it = ll.iterator();
        while(it.hasNext()){
            Student s = it.next();
            System.out.println(" xingm "+s.getName()+" nianl "+s.getAge());
        }
        System.out.println(" 545555555555555555555555555 ");
        for(Student s:ll){
            System.out.println(" xingm "+s.getName()+" nianl "+s.getAge());
        }
       

扫码关注一起随时随地学习!!!就在洋葱攻城狮,更多精彩,等你来!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋葱ycy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值