javaSE.Set

Set接口

特点:无序,无下标,元素不可重复;
方法:全部继承自Collection;

HashSet(重点)

基于HashCode实现元素不重复;
当存入元素的哈希码相同时,会调用equals进行确认,如为true,则拒绝后者进入:
1.根据hashcode计算保存的位置,如果位置为空,则直接保存,如果不为空执行第二步;
2.执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表存储。

/**
 * HashSet集合的使用
 * 存储结构:哈希表(数组+链表+红黑树)
 * 基于HashCode实现元素不重复;
 * 当存入元素的哈希码相同时,会调用equals进行确认,如为true,则拒绝后者进入:
 *   1.根据hashcode计算保存的位置,如果位置为空,则直接保存,如果不为空执行第二步;
 *   2.执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表存储。
 */
public class HashSetDemo {
    public static void main(String[] args) {
        //创建集合
        HashSet<String> hashSet = new HashSet<>();
        //1.添加元素
        hashSet.add("tom");
        hashSet.add("jack");
        hashSet.add("jason");
        hashSet.add("tom");
        System.out.println("元素个数:"+hashSet.size());
        System.out.println(hashSet.toString());
        //2.删除元素
        hashSet.remove("jason");
        System.out.println("删除jason后元素个数:"+hashSet.size());
        //3.遍历操作
        System.out.println("------3.1 增强for-------");
        for (String str : hashSet ) {
            System.out.println(str);

        }
        System.out.println("------3.2 迭代器---------");
        Iterator<String> iterator=hashSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //4.判断
        hashSet.contains("rose");
        System.out.println(hashSet.isEmpty());

        HashSet hashSet1 = new HashSet();
        Student s1 = new Student("tom", 1);
        Student s2 = new Student("jack", 2);
        Student s3 = new Student("rose", 3);
        hashSet1.add(s1);
        hashSet1.add(s2);
        hashSet1.add(s3);
        hashSet1.add(s3);
        System.out.println("元素个数:"+hashSet1.size());
        Student s4=new Student("rose", 3);
        hashSet1.add(s4);
        System.out.println("元素个数:"+hashSet1.size());
        System.out.println(s3.hashCode()==s4.hashCode());
    }
}

TreeSet

基于排列顺序实现元素不重复;
实现了SortedSet接口,对集合元素自动排序;
元素对象的类型必须实现Comparable接口,指定排序规则;
通过CompareTo方法确定是否为重复元素。

/**
 * 演示treeSet使用
 * 存储结构:红黑树
 * 要求:元素必须实现Comparable接口,compareTo()方法返回值为0,判定为相同元素
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        //创建集合
        TreeSet<String> treeSet = new TreeSet<>();
        //1.添加元素
        treeSet.add("xyz");
        treeSet.add("abc");
        treeSet.add("hello");
        System.out.println("添加前元素个数:"+treeSet.size());
        System.out.println(treeSet.toString());
        treeSet.add("xyz");//无法添加相同元素
        System.out.println("添加后元素个数:"+treeSet.size());
        //2.删除
        treeSet.remove("xyz");
        System.out.println("删除后元素个数:"+treeSet.size());
        //3.遍历
        System.out.println("---3.1 使用增强for---");
        for (String str:treeSet) {
            System.out.println(str);
        }
        System.out.println("---3.2 使用迭代器---");
        Iterator<String> iterator = treeSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //4.判断
        System.out.println(treeSet.contains("abc"));
        System.out.println("===应用类型元素===");
        TreeSet<Person> persons=new TreeSet<>();
        Person p1=new Person("tom",20);
        Person p2 = new Person("joe", 21);
        Person p3=new Person("bob",22);
        //Person必须实现Comparable接口才能添加
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);
        System.out.println("元素个数:"+persons.size());
        System.out.println(persons.toString());
        persons.add(p1);
        System.out.println("元素个数:"+persons.size());
        persons.remove(p3);
        //移除不了,除非重写Person的hashCode()和equals()
        persons.remove(new Person("joe", 20));
    }
}

Comparator(比较器)

public class ComparatorDemo {
    public static void main(String[] args) {
        //创建集合,并指定比较规则
        TreeSet<Person> persons=new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                int n1=o1.getAge()-o2.getAge();
                int n2=o1.getName().compareTo(o2.getName());
                return n1==0?n2:n1;
            }
        });
        Person p1 = new Person("xyz", 20);
        Person p2=new Person("abc",21);
        Person p3=new Person("def",19);
        Person p4=new Person("lmn",21);
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);
        persons.add(p4);
        System.out.println(persons.toString());
    }
}
posted @ 2022-03-01 14:02  老李学Java  阅读(26)  评论(0)    收藏  举报