【Java】Set集合的基本使用

  📝个人主页:哈__

期待您的关注 

一、HashSet集合

1.HashSet集合的特点

2.HashSet常用方法

①:add(Object o):向Set集合中添加元素,不允许添加重复数据。

②:size():返回Set集合中的元素个数

public class Test {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>(); //调用HashSet无参构造方法——>创建HashMap对象并给map全局变量。
        set.add("张三");
        set.add("李四");
        set.add("王五");
        set.add("王五");
        System.out.println(set);
        System.out.println(set.size());
    }
}

注意:不会按照保存的顺序存储数据(顺序不定),遍历时不能保证下次结果和上次相同。且向HashSet集合中添加元素,HashSet add方法实质是map全局变量调用了put方法,将数据存到了key,因为HashMap的 key不允许,所以HashSet添加的元素也不允许重复。

③.remove(Object o): 删除Set集合中的obj对象,删除成功返回true,否则返回false。

④.isEmpty():如果Set不包含元素,则返回 true。

 public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>();
        set.add("张三");
        set.add("李四");
        System.out.println(set.isEmpty());
        System.out.println(set.remove("张三"));
        System.out.println(set.remove("张三"));
        System.out.println(set);
    }

 

⑤.clear(): 移除此Set中的所有元素。

 public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>();
        set.add("张三");
        set.add("李四");
        System.out.println(set);
        set.clear();
        System.out.println(set);
    }

⑥.iterator():返回在此Set中的元素上进行迭代的迭代器。

public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>();
        set.add("张三");
        set.add("李四");
        Iterator<String> ite =set.iterator();
        while(ite.hasNext())
        {
            System.out.println(ite.next());
        }
    }

 

⑦.contains(Object o):判断集合中是否包含obj元素。

 public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>();
        set.add("张三");
        set.add("李四");
        System.out.println(set.contains("张三"));
        System.out.println(set.contains("王五"));
    }

⑧:加强for循环遍历Set集合

public static void main(String[] args) {
        HashSet<String> set = new HashSet<String>();
        set.add("张三");
        set.add("李四");
        for (String name : set) {   //使用foreach进行遍历。
            System.out.println(name);
        }
    }

二、LinkedHashSet集合

LinkedHashSet集合的特点

三、TreeSet集合

1.TreeSet集合的特点

 

2.TreeSet的基本使用

①.插入是按字典序排序的

public static void main(String[] args) {
        TreeSet ts=new TreeSet();
        ts.add("agg");
        ts.add("abcd");
        ts.add("ffas");
        Iterator it=ts.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }

 ②.如果插入的是自定义对象 需要让类实现 Comparable 接口并且必须要重写compareTo

class Person implements Comparable{
    
    String name;
    int age;
    Person(String name,int age)
    {
        this.name=name;
        this.age=age;
    }

    @Override
    public int compareTo(Object o) {
        Person p=(Person)o;
        //先对姓名字典序比较 如果相同 比较年龄
        if(this.name.compareTo(p.name)!=0) {
            return this.name.compareTo(p.name);
        }
        else
        {
            if(this.age>p.age) return 1;
            else if(this.age<p.age) return -1;
        }
        return 0;
    }

}

public class Test {
    public static void main(String args[])
    {
        TreeSet ts=new TreeSet();
        ts.add(new Person("agg",21));
        ts.add(new Person("abcd",12));
        ts.add(new Person("ffas",8));
        ts.add(new Person("agg",12));
        Iterator it=ts.iterator();
        while(it.hasNext())
        {
            Person p=(Person)it.next();
            System.out.println(p.name+":"+p.age);
        }
    }
}

版权声明:本文为博主作者:哈__原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/qq_61024956/article/details/137711089

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2024年4月22日
下一篇 2024年4月22日

相关推荐