黑马程序员——Java基础——集合(二)

来源:互联网 发布:篮球比赛竞猜软件 编辑:程序博客网 时间:2024/06/06 09:21

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
一、Set集合
Set集合是List的子类,是无序的(存储顺序和取出顺序不一致),元素不可重复

Set 无序,唯一        |--HashSet            底层数据结构是哈希表。            hashCode()和equals()保证元素唯一        |--TreeSet            底层数据结构是红黑树。            保证元素排序                自然排序                比较器排序            保证元素唯一性                根据比较的返回值是否是0来决定

A:HashSet
线程不安全,存取速度快
可以通过元素的两个方法,hashCode()和equals()来保证元素的唯一性,如果元素的hashCode 值相同,才会判断equals是否为true,如果元素的hashCode值不同,不会调用equals。
注意:存储自定义对象时要保证元素的唯一性需要重写hashCode()方法和equals()方法。

/*     * 需求:存储自定义对象,并保证元素的唯一性     * 要求:如果两个对象的成员变量值都相同,则为同一个元素。     *      * 目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。     * 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类。     * 这个时候,他们的哈希值是不会一样的,根本就不会继续判断,执行了添加操作。     *      */    class HashSetDemo2 {        public static void main(String[] args) {            // 创建集合对象            HashSet<Student> hs = new HashSet<Student>();            // 创建学生对象            Student s1 = new Student("林青霞", 27);            Student s2 = new Student("柳岩", 22);            Student s3 = new Student("王祖贤", 30);            Student s4 = new Student("林青霞", 27);            Student s5 = new Student("林青霞", 20);            Student s6 = new Student("范冰冰", 22);            // 添加元素            hs.add(s1);            hs.add(s2);            hs.add(s3);            hs.add(s4);            hs.add(s5);            hs.add(s6);            // 遍历集合            for (Student s : hs) {                System.out.println(s.getName() + "---" + s.getAge());            }        }    }    //学生类     class Student {        private String name;        private int age;        public Student() {            super();        }        public Student(String name, int age) {            super();            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;        }        //重写hashCode()和equals()方法        @Override        public int hashCode() {            final int prime = 31;            int result = 1;            result = prime * result + age;            result = prime * result + ((name == null) ? 0 : name.hashCode());            return result;        }        @Override        public boolean equals(Object obj) {            if (this == obj)                return true;            if (obj == null)                return false;            if (getClass() != obj.getClass())                return false;            Student other = (Student) obj;            if (age != other.age)                return false;            if (name == null) {                if (other.name != null)                    return false;            } else if (!name.equals(other.name))                return false;            return true;        }

B:TreeSet
底层数据结构是二叉树结构(红黑树结构)
a:保证元素唯一性的依据:通过compareTo()方法的返回值,是正整数、负整数还是0,则两个对象较大、较小或相同。相同则不会存入。
b:TreeSet排序的两种方式
自然排序:
让元素本身具有排序性。元素本身需要实现Comparable接口,重写compareTo()方法。
示例:

    /*     * TreeSet存储自定义对象并保证排序和唯一。     *      自然排序,按照年龄从小到大排序     *      成员变量值都相同即为同一个元素     */    class TreeSetDemo2 {        public static void main(String[] args) {            // 创建集合对象            TreeSet<Student> ts = new TreeSet<Student>();            // 创建元素            Student s1 = new Student("linqingxia", 27);            Student s2 = new Student("zhangguorong", 29);            Student s3 = new Student("wanglihong", 23);            Student s4 = new Student("linqingxia", 27);            Student s5 = new Student("liushishi", 22);            Student s6 = new Student("wuqilong", 40);            Student s7 = new Student("fengqingy", 22);            // 添加元素            ts.add(s1);            ts.add(s2);            ts.add(s3);            ts.add(s4);            ts.add(s5);            ts.add(s6);            ts.add(s7);            // 遍历            for (Student s : ts) {                System.out.println(s.getName() + "---" + s.getAge());            }        }    }    //自定义学生类    class Student implements Comparable<Student> {        private String name;        private int age;        public Student() {            super();        }        public Student(String name, int age) {            super();            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;        }        //重写compareTo()方法        @Override        public int compareTo(Student s) {            // 按照年龄排序,主要条件            int num = this.age - s.age;            // 次要条件            // 年龄相同的时候,还得去看姓名是否也相同            // 如果年龄和姓名都相同,才是同一个元素            int num2 = num == 0 ? this.name.compareTo(s.name) : num;            return num2;        }    }

比较器排序:集合本身具有比较性
在集合初始化时就具有了比较性,定义一个比较器实现Comparator接口,重写compare()方法,将比较器对象作为参数传递给集合构造函数。
示例:

/*     * 需求:请按照姓名的长度排序     */    class TreeSetDemo {        public static void main(String[] args) {            // 创建集合对象        // public TreeSet(Comparator comparator) //比较器排序        TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());        // 创建元素        Student s1 = new Student("linqingxia", 27);        Student s2 = new Student("zhangguorong", 29);        Student s3 = new Student("wanglihong", 23);        Student s4 = new Student("linqingxia", 27);        Student s5 = new Student("liushishi", 22);        Student s6 = new Student("wuqilong", 40);        Student s7 = new Student("fengqingy", 22);        Student s8 = new Student("linqingxia", 29);        // 添加元素        ts.add(s1);        ts.add(s2);        ts.add(s3);        ts.add(s4);        ts.add(s5);        ts.add(s6);        ts.add(s7);        ts.add(s8);        // 遍历        for (Student s : ts) {            System.out.println(s.getName() + "---" + s.getAge());        }    }    }    //自定义学生类    class Student {        private String name;        private int age;        public Student() {            super();        }        public Student(String name, int age) {            super();            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;        }    }    //自定义比较器    class MyComparator implements Comparator<Student> {        @Override        public int compare(Student s1, Student s2) {            // 姓名长度            int num = s1.getName().length() - s2.getName().length();            // 姓名内容            int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;            // 年龄            int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;            return num3;        }        }
0 0
原创粉丝点击