Set集合的特点

Set系列集合特点:

无序:添加数据的顺序和获取出的数据顺序不一致;不重复;无索引;

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

LinkedHashSet:有序,不重复,无索引

TreeSet:排序,不重复,无索引

 HashSet:


import java.util.*;

public class Work1 {
    public static void main(String[] args)  {
       //1,创建一个Set集合的对象
        //创建了一个HashSet的集合对象 //无序,不重复,无索引
        Set set = new HashSet<>();
        set.add(666);
        set.add(555);
        set.add(555);
        set.add(888);
        set.add(888);
        set.add(777);
        set.add(777);
        System.out.println(set);
    }
}

 LinkedHashSet:

import java.util.*;

public class Work1 {
    public static void main(String[] args)  {
        Set set = new LinkedHashSet<>();//有序 不重复 无索引
        set.add(666);
        set.add(555);
        set.add(555);
        set.add(888);
        set.add(888);
        set.add(777);
        set.add(777);
        System.out.println(set);


    }
}

TreeSet:

import java.util.*;

public class Work1 {
    public static void main(String[] args)  {
        Set set = new TreeSet<>();//排序(默认升序) 不重复 无索引
        set.add(666);
        set.add(555);
        set.add(555);
        set.add(888);
        set.add(888);
        set.add(777);
        set.add(777);
        System.out.println(set);
    }
}

HashSet集合的底层原理

Set集合的特点_第1张图片

 

public class Work1 {
    public static void main(String[] args)  {
       Studnt s1 = new Studnt("简隋英",25,188);
       Studnt s2 = new Studnt("李玉",18,188);
        System.out.println(s1.hashCode());
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }

}
class Studnt{
    private String name;
    private int age;
    private double height;

    public Studnt() {
    }

    public Studnt(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    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;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}

 

 

 

你可能感兴趣的:(java,开发语言)