Java基础——Set集合实现类

(1)Set集合实现类特点:

  • HashSet:无序,不重复,无索引。
  • LinkedHashSet:有序,不重复,无索引。
  • TreeSet:排序,不重复,无索引。

 (2)HashSet集合

1.底层原理:

  • HashSet集合底层采取哈希表存储的数据。
  • 哈希表是一种对于增删改查数据性能都较好的结构。

2.哈希表的组成: 

  • JDK8开始后,底层采用数组+链表+红黑树组成。
  • JDK8之前的,底层使用数组+链表组成。

3.哈希值:

  • 是JDK根据对象的地址,按照某种规则算出来的int类型的数值。

4.Object类的API:

  • public int hashCode():返回对象的哈希值。

5.对象的哈希值特点:

  • 同一个对象多次调用hashCode()方法返回的哈希值是相同的。
  • 默认情况下,不同对象的哈希值是不同的。 
  • public class HashDemo01 {
        public static void main(String[] args) {
            //1.获取对象的哈希值
            String name1 = "大大";
            System.out.println(name1.hashCode());//730336
            //同一个对象多次调用hashCode()方法返回的哈希值是相同的。
            System.out.println(name1.hashCode());//730336
    
            //默认情况下,不同对象的哈希值是不同的。
            String name2 = "小小";
            System.out.println(name2.hashCode());//754144
        }
    }
    

 (3)LinkedHashSet集合:

  • 有序,不重复,无索引。
  • 有序:保证存储和取出的元素顺序一致。
  • 原理:底层数据结构依然是哈希值,只是每个元素又额外的多了一个双链表的机制记录存储的顺序。

 (4)TreeSet集合:

1.TreeSet概述特点:

  • 不重复,无索引,可排序。
  • 可排序:按照元素的大小默认升序(有小到大)排序。
  • TreeSet集合底层是基于红黑树的数据结构实现排序的,增删改查性能都较好。
  • 注意:TreeSet集合是一定要排序的,可以将元素按照指定的规则进行排序。

2.TreeSet集合排序规则:

  • 对于数值类型:Interge,Double,官方默认按照大小进行升序排序。
  • 对于字符串类型:默认按照首字符的编号升序排序。
  • 对于自定义类型如Student对象,TreeSet无法直接排序。
  • 注意:想要使用TreeSet存储自定义类型,需要制定排序规则

3.TreeSet集合存储对象自定义排序规则方式:

  • 方式一:让自定义的类(如学生类)实现Comparable接口重写里面的comparaTo方法来制定比较规则。
  •  方式二:TreeSet集合有参数构造器,可以设置Comparator接口对应的比较器对象,来制定比较规则。
  • 注意:如果TreeSet集合存储的对象有实现比较规则,集合也自带比较器,默认使用自带的比较器排序
    
    • 两种方式返回值规则:
      • 如果认为第一个元素大于第二个元素返回正整数即可。
      • 如果认为第一个元素小于第二个元素返回负整数即可。
      • 如果认为第一个元素等于第二个元素返回0即可,此时TreeSet集合只会保留一个元素,认为两者重复。
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * 目标:观察TreeSet集合的排序规则
 * TreeSet集合特点:不重复,无索引,可排序
 */
public class TreeSetDemo01 {
    public static void main(String[] args) {
        //1.对于数值类型:Interge,Double,官方默认按照大小进行升序排序。
        Set<Integer> sets = new TreeSet<>();
        sets.add(20);
        sets.add(25);
        sets.add(22);
        sets.add(19);
        System.out.println(sets);//[19, 20, 22, 25]

        //2.对于字符串类型:默认按照首字符的编号升序排序。
        Set<String> sets1 = new TreeSet<>();
        sets1.add("Aaa");
        sets1.add("Bbb");
        sets1.add("Aaa");
        sets1.add("DFG");
        sets1.add("CFG");
        System.out.println(sets1);//[Aaa, Bbb, CFG, DFG]

        //3.对于自定义类型如Student对象,TreeSet无法直接排序。想要使用TreeSet存储自定义类型,需要制定排序规则
        Set<Apple> apples = new TreeSet<>();
        apples.add(new Apple("苹果1","红色",9.9,500));
        apples.add(new Apple("苹果2","绿色",19.9,400));
        apples.add(new Apple("苹果3","青色",29.9,300));
        apples.add(new Apple("苹果4","黄色",39.9,200));

        /**
         * 自定义规则
         * 方式一:让自定义的类(如学生类)实现Comparable接口重写里面的comparaTo方法来制定比较规则。
          */
        System.out.println(apples);
        //[Apple{name='苹果4', color='黄色', price=39.9, weight=200}, Apple{name='苹果3', color='青色', price=29.9, weight=300}, Apple{name='苹果2', color='绿色', price=19.9, weight=400}, Apple{name='苹果1', color='红色', price=9.9, weight=500}]

//        /**
//         * 自定义比较规则    集合自带比较器
//         * 方式二:TreeSet集合有参数构造器,可以设置Comparator接口对应的比较器对象,来制定比较规则。
//         */
//        Set<Apple> apples1 = new TreeSet<>(new Comparator<Apple>() {
//            @Override
//            public int compare(Apple o1, Apple o2) {
                return o1.getWeight() - o2.getWeight();//升序
                return o2.getWeight() - o1.getWeight();//降序
                return Double.compare(o2.getPrice() , o1.getPrice());//降序
//                return Double.compare(o1.getPrice() , o2.getPrice());//升序
//            }
//        });

        //简化代码
        Set<Apple> apples1 = new TreeSet<>(( o1,  o2) -> Double.compare(o1.getPrice() , o2.getPrice()));

        apples1.add(new Apple("苹果1","红色",9.9,500));
        apples1.add(new Apple("苹果2","绿色",19.9,400));
        apples1.add(new Apple("苹果3","青色",29.9,300));
        apples1.add(new Apple("苹果4","黄色",39.9,200));
        System.out.println(apples1);
        //[Apple{name='苹果1', color='红色', price=9.9, weight=500}, Apple{name='苹果2', color='绿色', price=19.9, weight=400}, Apple{name='苹果3', color='青色', price=29.9, weight=300}, Apple{name='苹果4', color='黄色', price=39.9, weight=200}]


        //如果TreeSet集合存储的对象有实现比较规则,集合也自带比较器,默认使用自带的比较器排序

    }
}

class Apple implements Comparable<Apple>{
    private String name;
    private String color;
    private Double price;
    private int weight;

    public Apple() {
    }

    public Apple(String name, String color, Double price, int weight) {
        this.name = name;
        this.color = color;
        this.price = price;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                ", weight=" + weight +
                '}';
    }

    /**
     * 自定义制定规则:方式一
     * o1.compareTo(o2)
     * @param o
     * @return如果认为第一个元素大于第二个元素返回正整数即可。
     * 如果认为第一个元素小于第二个元素返回负整数即可。
     * 如果认为第一个元素等于第二个元素返回0即可,此时TreeSet集合只会保留一个元素,认为两者重复。
     */
    @Override
    public int compareTo(Apple o) {
        //如:按照重量进行比较
        //return this.weight - o.weight;//会去掉重量重复的元素
        return this.weight - o.weight >= 0 ? 1 : -1;//可以保留重量重复的元素
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_61275790/article/details/130029294