Java基础-17(01)总结,TreeSet,LinkHashSet

时间:2022-05-04
本文章向大家介绍Java基础-17(01)总结,TreeSet,LinkHashSet,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
(3)TreeSet集合

A:底层数据结构是红黑树(是一个自平衡的二叉树)

B:保证元素的排序方式

a:自然排序(元素具备比较性)

让元素所属的类实现Comparable接口

package cn.itcast_05;

import java.util.TreeSet;
/*
 * TreeSet:能够对元素按照某种规则进行排序。
 * 排序有两种方式
 * A:自然排序
 * B:比较器排序
 * 
 * TreeSet集合的特点:排序和唯一
 * 
 * 通过观察TreeSet的add()方法,我们知道最终要看TreeMap的put()方法。
 */
public class TreeSetDemo {
 public static void main(String[] args) {
 // 创建集合对象
 // 自然顺序进行排序
 TreeSet<Integer> ts = new TreeSet<Integer>();
 // 创建元素并添加
 // 20,18,23,22,17,24,19,18,24
 ts.add(20);
 ts.add(18);
 ts.add(23);
 ts.add(22);
 ts.add(17);
 ts.add(24);
 ts.add(19);
 ts.add(18);
 ts.add(24);
 // 遍历
 for (Integer i : ts) {
 System.out.println(i);
 }
 }
}
 b:比较器排序(集合具备比较性)
 让集合构造方法接收Comparator的实现类对象
package cn.itcast_07;
import java.util.Comparator;
public class MyComparator implements Comparator<Student> {
 @Override
 public int compare(Student s1, Student s2) {
 // int num = this.name.length() - s.name.length();
 // this -- s1
 // s -- 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;
 }
}
 C:把我们讲过的代码看一遍即可
 D:TreeSet的add()方法的源码解析
interface Collection {...}
interface Set extends Collection {...}
interface NavigableMap {
}
class TreeMap implements NavigableMap {
  public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check
            root = new Entry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        Entry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        else {
            if (key == null)
                throw new NullPointerException();
            Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        Entry<K,V> e = new Entry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }
}
class TreeSet implements Set {
 private transient NavigableMap<E,Object> m;
 public TreeSet() {
  this(new TreeMap<E,Object>());
 }
 public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }
}
真正的比较是依赖于元素的compareTo()方法,而这个方法是定义在 Comparable里面的。
所以,你要想重写该方法,就必须是先 Comparable接口。这个接口表示的就是自然排序。
 (4)案例:
 A:获取无重复的随机数
package cn.itcast_08;
import java.util.HashSet;
import java.util.Random;
/*
 * 编写一个程序,获取10个1至20的随机数,要求随机数不能重复。
 * 
 * 分析:
 *  A:创建随机数对象
 *  B:创建一个HashSet集合
 *  C:判断集合的长度是不是小于10
 *  是:就创建一个随机数添加
 *  否:不搭理它
 *  D:遍历HashSet集合
 */
public class HashSetDemo {
 public static void main(String[] args) {
 // 创建随机数对象
 Random r = new Random();
 // 创建一个Set集合
 HashSet<Integer> ts = new HashSet<Integer>();
 // 判断集合的长度是不是小于10
 while (ts.size() < 10) {
 int num = r.nextInt(20) + 1;
 ts.add(num);
 }
 // 遍历Set集合
 for (Integer i : ts) {
 System.out.println(i);
 }
 }
}
 B:键盘录入学生按照总分从高到底输出
package cn.itcast_08;(1)
public class Student {
 // 姓名
 private String name;
 // 语文成绩
 private int chinese;
 // 数学成绩
 private int math;
 // 英语成绩
 private int english;
 public Student(String name, int chinese, int math, int english) {
 super();
 this.name = name;
 this.chinese = chinese;
 this.math = math;
 this.english = english;
 }
 public Student() {
 super();
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getChinese() {
 return chinese;
 }
 public void setChinese(int chinese) {
 this.chinese = chinese;
 }
 public int getMath() {
 return math;
 }
 public void setMath(int math) {
 this.math = math;
 }
 public int getEnglish() {
 return english;
 }
 public void setEnglish(int english) {
 this.english = english;
 }
 public int getSum() {
 return this.chinese + this.math + this.english;
 }
}
package cn.itcast_08;(2)
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/*
 * 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
 * 
 * 分析:
 *  A:定义学生类
 *  B:创建一个TreeSet集合
 *  C:总分从高到底如何实现呢? 
 *  D:键盘录入5个学生信息
 *  E:遍历TreeSet集合
 */
public class TreeSetDemo {
 public static void main(String[] args) {
 // 创建一个TreeSet集合
 TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
 @Override
 public int compare(Student s1, Student s2) {
 // 总分从高到低
 int num = s2.getSum() - s1.getSum();
 // 总分相同的不一定语文相同
 int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
 // 总分相同的不一定数序相同
 int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
 // 总分相同的不一定英语相同
 int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
 // 姓名还不一定相同呢
 int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
 : num4;
 return num5;
 }
 });
 System.out.println("学生信息录入开始");
 // 键盘录入5个学生信息
 for (int x = 1; x <= 5; x++) {
 Scanner sc = new Scanner(System.in);
 System.out.println("请输入第" + x + "个学生的姓名:");
 String name = sc.nextLine();
 System.out.println("请输入第" + x + "个学生的语文成绩:");
 String chineseString = sc.nextLine();
 System.out.println("请输入第" + x + "个学生的数学成绩:");
 String mathString = sc.nextLine();
 System.out.println("请输入第" + x + "个学生的英语成绩:");
 String englishString = sc.nextLine();
 // 把数据封装到学生对象中
 Student s = new Student();
 s.setName(name);
 s.setChinese(Integer.parseInt(chineseString));
 s.setMath(Integer.parseInt(mathString));
 s.setEnglish(Integer.parseInt(englishString));
 // 把学生对象添加到集合
 ts.add(s);
 }
 System.out.println("学生信息录入完毕");
 System.out.println("学习信息从高到低排序如下:");
 System.out.println("姓名t语文成绩t数学成绩t英语成绩");
 // 遍历集合
 for (Student s : ts) {
 System.out.println(s.getName() + "t" + s.getChinese() + "t"
 + s.getMath() + "t" + s.getEnglish());
 }
 }
}
 C(1):请按照姓名的长度排序(自然排序Comparable)(主要判断条件年龄在判断姓名长度)
package cn.itcast_05;(1)
/*
 * 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口
 */
public 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;
 }
 @Override
 public int compareTo(Student s) {
 // return 0;
 // return 1;
 // return -1;
 // 这里返回什么,其实应该根据我的排序规则来做
 // 按照年龄排序,主要条件
 int num = this.age - s.age;
 // 次要条件
 // 年龄相同的时候,还得去看姓名是否也相同
 // 如果年龄和姓名都相同,才是同一个元素
 int num2 = num == 0 ? this.name.compareTo(s.name) : num;
 return num2;
 }
}
package cn.itcast_05;(2)
import java.util.TreeSet;
/*
 * TreeSet存储自定义对象并保证排序和唯一。
 * 
 * A:你没有告诉我们怎么排序
 *  自然排序,按照年龄从小到大排序
 * B:元素什么情况算唯一你也没告诉我
 *  成员变量值都相同即为同一个元素
 */
public 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());
 }
 }
}
 C(2):请按照姓名的长度排序(自然排序Comparable)(主要判断条件姓名长度在判断年龄)
package cn.itcast_06;(1)
/*
 * 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口
 */
public 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;
 }
 @Override
 public int compareTo(Student s) {
 // 主要条件 姓名的长度
 int num = this.name.length() - s.name.length();
 // 姓名的长度相同,不代表姓名的内容相同
 int num2 = num == 0 ? this.name.compareTo(s.name) : num;
 // 姓名的长度和内容相同,不代表年龄相同,所以还得继续判断年龄
 int num3 = num2 == 0 ? this.age - s.age : num2;
 return num3;
 }
}
package cn.itcast_06;(2)
import java.util.TreeSet;
/*
 * 需求:请按照姓名的长度排序
 */
public class TreeSetDemo {
 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);
 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());
 }
 }
}
 C(3):请按照姓名的长度排序(比较器排序Comparator)(主要条件姓名长度在判断年龄)
package cn.itcast_07;(1)
public 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;
 }
}
package cn.itcast_07;(2)
import java.util.Comparator;
import java.util.TreeSet;
/*
 * 需求:请按照姓名的长度排序
 * 
 * TreeSet集合保证元素排序和唯一性的原理
 * 唯一性:是根据比较的返回是否是0来决定。
 * 排序:
 *  A:自然排序(元素具备比较性)
 *  让元素所属的类实现自然排序接口 Comparable
 *  B:比较器排序(集合具备比较性)
 *  让集合的构造方法接收一个比较器接口的子类对象 Comparator
 */
public class TreeSetDemo {
 public static void main(String[] args) {
 // 创建集合对象
 // TreeSet<Student> ts = new TreeSet<Student>(); //自然排序
 // public TreeSet(Comparator comparator) //比较器排序
 // TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
 // 如果一个方法的参数是接口,那么真正要的是接口的实现类的对象
 // 而匿名内部类就可以实现这个东西
 TreeSet<Student> ts = new TreeSet<Student>(new 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;
 }
 });
 // 创建元素
 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());
 }
 }
}

(5)LinkHashSet集合

package cn.itcast_04;
import java.util.LinkedHashSet;
/*
 * LinkedHashSet:底层数据结构由哈希表和链表组成。
 * 哈希表保证元素的唯一性。
 * 链表保证元素有素。(存储和取出是一致)
 */
public class LinkedHashSetDemo {
 public static void main(String[] args) {
 // 创建集合对象
 LinkedHashSet<String> hs = new LinkedHashSet<String>();
 // 创建并添加元素
 hs.add("hello");
 hs.add("world");
 hs.add("java");
 hs.add("world");
 hs.add("java");
 // 遍历
 for (String s : hs) {
 System.out.println(s);
 }
 }
}

3:Collection集合总结(掌握)

Collection

|--List 有序,可重复

|--ArrayList

底层数据结构是数组,查询快,增删慢。

线程不安全,效率高

|--Vector

底层数据结构是数组,查询快,增删慢。

线程安全,效率低

|--LinkedList

底层数据结构是链表,查询慢,增删快。

线程不安全,效率高

|--Set 无序,唯一

|--HashSet

底层数据结构是哈希表。

如何保证元素唯一性的呢?

依赖两个方法:hashCode()和equals()

开发中自动生成这两个方法即可

|--LinkedHashSet

底层数据结构是链表和哈希表

由链表保证元素有序

由哈希表保证元素唯一

|--TreeSet

底层数据结构是红黑树。

如何保证元素排序的呢?

自然排序

比较器排序

如何保证元素唯一性的呢?

根据比较的返回值是否是0来决定

4:针对Collection集合我们到底使用谁呢?(掌握)

唯一吗?

是:Set

排序吗?

是:TreeSet

否:HashSet

如果你知道是Set,但是不知道是哪个Set,就用HashSet。

否:List

要安全吗?

是:Vector

否:ArrayList或者LinkedList

查询多:ArrayList

增删多:LinkedList

如果你知道是List,但是不知道是哪个List,就用ArrayList。

如果你知道是Collection集合,但是不知道使用谁,就用ArrayList。

如果你知道用集合,就用ArrayList。

5:在集合中常见的数据结构(掌握)

ArrayXxx:底层数据结构是数组,查询快,增删慢

LinkedXxx:底层数据结构是链表,查询慢,增删快

HashXxx:底层数据结构是哈希表。依赖两个方法:hashCode()和equals()

TreeXxx:底层数据结构是二叉树。两种方式排序:自然排序和比较器排序