• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

来学java数据结构——Set基础

武飞扬头像
CAFE~BABE
帮助1

【一起来学java数据结构】——Set


Set是一个在Collection下的一个数据结构。

它和Map很像,不同点就是:Set实现了Collection,而Map并没有实现它。

更重要的一点是:Set里面只是存储了Key,没有存储Value

学新通

Set的方法

下面就先来介绍一下set的一些基本方法

public static void main(String[] args) {
        Set<Integer> set=new HashSet<>();
        set.add(1);
        System.out.println(set.add(2));//true
        System.out.println(set.contains(1));//true
        System.out.println(set.contains(100));//false
        System.out.println(set.size());//2
        System.out.println(set.remove(100));//false
        System.out.println(set.remove(1));//true
        System.out.println(set.isEmpty());//false
        set.clear();
        System.out.println(set.isEmpty());//true

}

toArray

将set里的值转换成Array

    public static void main(String[] args) {
        Set<Integer> set=new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        Object[] objects= set.toArray();
        for (Object o:objects) {
            System.out.print(o " ");
        }
    }
----------------------------------------------------
    //还可以使用下面的代码
     Integer[] integers=set.toArray(new Integer[0]);
        for (Integer integer:integers) {
            System.out.print(integer " ");
        }
学新通

Iterator

这是一个迭代器和while一起使用

    public static void main(String[] args) {
        Set<Integer> set=new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        Iterator<Integer> iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

containsAll

    public static void main(String[] args) {
        Set<Integer> set=new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        System.out.println(set.containsAll(list));//true
        list.add(0);
        System.out.println(set.containsAll(list));//false
    }

检查Set里面是不是包含其他集合里面的元素

addAll

将另一个集合的内容加入到Set中,并会自动去重

    public static void main(String[] args) {
        Set<Integer> set=new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        set.addAll(list);
        System.out.println(set);// 1 2 3 4
        list.add(0);
        set.addAll(list);
        System.out.println(set);//0 1 2 3 4
    }

Set的特点

  1. 和Map不一样,Set本身是继承了Collection接口的
  2. 实现Set接口的常用类是HashSet和TreeSet,还用一个不常用的类是LinkedHashSet
  3. Set里面只存放Key,不存放Value
  4. Set里面没有提供修改Key的方法,所以只能删掉再重新插入
  5. key的值不可以相同且不可以为null
  6. Set的最大的作用是去重

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfifjjf
系列文章
更多 icon
同类精品
更多 icon
继续加载