HashSet、LinkedHashSet、TreeSet

1、去重复、按add顺序输出、元素排序

package demo02;

import org.junit.Test;

import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/**
 * @description: demo01
 * @author: liuyang
 * @create: 2021-09-03 9:36
 */
public class Demo01 {
    @Test
    public void test1() {
        /**
         * 无序不可重复,对象需要重写equals和hashCode方法
         * 来确定是否重复的判断逻辑
         */
        Set<User> set = new HashSet<>();
        set.add(new User("AAA", 20));
        // 此元素添加失败,根据equals和hashCode方法的结果确定是否重复
        set.add(new User("AAA", 20));
        System.out.println(set);
    }

    @Test
    public void test2() {
        /**
         * LinkedHashSet可按照add元素的顺序遍历输出元素
         */
        Set<User> set = new LinkedHashSet<>();
        set.add(new User("CCC", 18));
        set.add(new User("BBB", 30));
        set.add(new User("AAA", 16));
        // 此元素添加失败,根据equals和hashCode方法的结果确定是否重复
        set.add(new User("AAA", 16));
        System.out.println(set);
    }

    @Test
    public void test3() {
        /**
         * TreeSet只能添加相同类型的元素,
         * 可自动将元素进行自然排序(前提是元素需要实现Comparable接口)
         */
        Set<String> set = new TreeSet<>();
        set.add("CCC");
        set.add("BBB");
        set.add("AAA");
        System.out.println(set); // [AAA, BBB, CCC]

        // 自然排序需实现Comparable接口重写compareTo方法
        Set<User> set1 = new TreeSet<>();
        set1.add(new User("CCC", 18));
        set1.add(new User("BBB", 30));
        set1.add(new User("AAA", 16));
        // 注意此时这个元素添加失败,因为此时不可重复取决于compareTo方法的结果
        set1.add(new User("AAA", 19));
        System.out.println(set1);

        // 定制排序,TreeSet构造器中传入Comparator接口的实例对象
        Set<User> set2 = new TreeSet<>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        set2.add(new User("CCC", 18));
        set2.add(new User("BBB", 30));
        set2.add(new User("AAA", 19));
        // 注意此时这个元素添加失败,因为此时不可重复取决于compare方法的结果
        set2.add(new User("AAA", 16));
        System.out.println(set2);
    }
}

class User implements Comparable<User> {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(User o) {
        return this.name.compareTo(o.name);
    }
}

 

posted @ 2021-09-03 10:09  牛牛的自留地  阅读(25)  评论(0编辑  收藏  举报