再多学一点吧

导航

集合Collection ----Set集合

set系列集合:添加的元素是 无序,不重复,无索引的

   ----HashSet: 无序,不重复,无索引

                  ------LinkHashSet<>:有序不重复无索引(添加顺序)

   ----TreeSet: 排序(按照大小默认升序排序,是可排序集合),不重复,无索引

 

HashSet:不重复,无序  

  set集合去重复的流程: 1.对象调用hashcode() 方法获取哈希值进行比较 false---不重复

            2.如果为true,则进行equals比较

    总结:如果set对象认为两个集合的内容一样就重复,重写hashcode()和equals方法

  set集合无序的原因是:底层采用了哈希表存储元素

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

class Sheep{
    private String name;
    private int age;
    Sheep(){
        
    }

    public Sheep(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;
        Sheep sheep = (Sheep) o;
        return age == sheep.age &&
                Objects.equals(name, sheep.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }
    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class HashSetDemo1 {
    public static void main(String[] args) {
        Set<Sheep> sets=new HashSet<>();
        Sheep s1 = new Sheep("懒洋洋",6);
        Sheep s2 = new Sheep("暖羊羊",8);
        Sheep s3 = new Sheep("懒洋洋",6);
        sets.add(s1);
        sets.add(s2);
        sets.add(s3);
        System.out.println(sets);  //  [Sheep{name='暖羊羊', age=8}, Sheep{name='懒洋洋', age=6}]
// 如果没有重写hashcode()和equals,则输出:[Sheep{name='暖羊羊', age=8}, Sheep{name='懒洋洋', age=6}, Sheep{name='懒洋洋', age=6}]
} }

LinkHashSet:每个元素都添加一个链来维护添加顺序

import java.util.LinkedHashSet;
import java.util.Set;
public class LinkHashSetDemo1 { public static void main(String[] args) { Set<String> sets = new LinkedHashSet<>(); sets.add("沸羊羊"); sets.add("喜羊羊"); sets.add("暖羊羊"); sets.add("喜羊羊"); System.out.println(sets); // [沸羊羊, 喜羊羊, 暖羊羊] } }

 

TreeSet:  

  1.有值特性的元素进行升序排序

  2.String排序按照首字母的 ASCII码值排序

       3.引用数据类型(默认无法排序,所以需要定制排序的大小规则) 

    a.直接为对象的类实现比较器规则接口Comparable,重写比较方法

    b.直接为集合设置比较器Comparator对象,重写比较方法

   总结:如果类和集合都带有比较规则,优先使用集合自带的比较规则

 

package com.day05;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;


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

    //重写比较方法 w1.compareTo(w)
    // 比较者:this   被比较者:w
    @Override
    public int compareTo(Wolf w){
        //比价规则!!
        //比较者大于被比较者,返回正数(1)
        //比较者小于被比较者,返回负数(-1)
        //比较者等于被比较者,返回0
//        if(this.age>w.age){
//            return 1;
//        }else if(this.age<w.age){
//            return -1;
//        }
//        return 0;
        return this.age-w.age;
    }


    Wolf(){

    }

    public Wolf(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 String toString() {
        return "Wolf{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class TreeSetDemo1 {
    public static void main(String[] args) {
//---------------第一种-------------------// 自然排序 Set
<Wolf> sets=new TreeSet<>(); sets.add(new Wolf("红太狼",24)); sets.add(new Wolf("灰太狼",28)); sets.add(new Wolf("小灰灰",12)); System.out.println(sets);        //--------------第二种--------------------// Set<Wolf> setss=new TreeSet<>(new Comparator<Wolf>() { // 匿名对象 比较器排序 @Override public int compare(Wolf o1, Wolf o2) { return o2.getAge()-o1.getAge(); } }); setss.add(new Wolf("红太狼",24)); setss.add(new Wolf("灰太狼",28)); setss.add(new Wolf("小灰灰",12)); System.out.println(setss); } }

 

package com.shujia.day18;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo1 {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> students = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {              //o1-o2为从小到大排序
                int i=o1.getName().length()-o2.getName().length();     
                int i1 = i == 0 ? o1.getName().compareTo(o2.getName()):i;
                int i2 = i1 == 0 ? o1.getAge() - o2.getAge():i1;
                return i2;
            }
        });


        //创建学生对象
        Student s1 = new Student("zhongqihang", 23);
        Student s2 = new Student("guoao", 24);
        Student s3 = new Student("sunduoqin", 28);
        Student s4 = new Student("kuangjie", 27);
        Student s5 = new Student("chenchuan", 26);
        Student s6 = new Student("wangyouhu", 29);
        Student s7 = new Student("chenzhiqiang", 29);
        Student s8 = new Student("chenchuan", 27);


        //添加元素
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.add(s6);
        students.add(s7);
        students.add(s8);

        //遍历
        for (Student s : students) {
            System.out.println(s.getName()+"----"+s.getAge());
        }

//guoao----24
//kuangjie----27
//chenchuan----26
//chenchuan----27
//sunduoqin----28
//wangyouhu----29
//zhongqihang----23
//chenzhiqiang----29

    }
}

 class Student {
    private String name;
    private int age;
    
    public Student() {
    }

    public Student(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;
    }
}

 

posted on 2021-08-08 21:43  糟糟张  阅读(77)  评论(0编辑  收藏  举报