栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java笔记 —— Set集合的排序原理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java笔记 —— Set集合的排序原理

TreeSet
      • 自然排序
    • TreeSet集合举例
      • 1.传入的元素为Integer类型时
  • 2. 传入的元素为自定义类的对象时

  • 相关源码

  • 自定义类继承Comparable接口

  • 比较器排序

      • 用匿名内部类的形式实现Comparator接口
  • 另外创建一个实现了Comparator接口的类

[](

)自然排序

[](

)TreeSet集合举例

[](

)1.传入的元素为Integer类型时

创建TreeSet集合对象时,会构造一个新的,空的树组,根据其元素的自然排序进行排序

无参构造内部元素默认是自然排序

import java.util.TreeSet;

public class demo3 {

public static void main(String[] args) {

TreeSet tree = new TreeSet<>();

tree.add(20);

tree.add(16);

tree.add(22);

tree.add(14);

tree.add(10);

tree.add(28);

for(Integer i : tree){

System.out.println(i);

}

}

}

结果为

会发现这里元素按从小到大的顺序排序了

[](

)2. 传入的元素为自定义类的对象时

import java.util.TreeSet;

public class demo4 {

public static void main(String[] args) {

TreeSet tree = new TreeSet<>();

Student2 s1 = new Student2(“zhang”,12);

Student2 s2 = new Student2(“zhou”,15);

Student2 s3 = new Student2(“cao”,16);

Student2 s4 = new Student2(“chen”,23);

Student2 s5 = new Student2(“zhang”,12);

tree.add(s1);

tree.add(s2);

tree.add(s3);

tree.add(s4);

tree.add(s5);

for(Student2 s : tree){

System.out.println(s.getName()+"—"+s.getAge());

}

}

}

结果是报错

所以,现在来看看TreeSet的相关源码

[](

)相关源码

public class TreeSet extends AbstractSet implements NavigableSet{

private transient NavigableMap m;

//调用无参构造方法生成的TreeSet对象,底层实际上是创建了一个TreeMap对象

public TreeSet() {

this(new TreeMap());

}

//TreeSet的add方法,实际上实现了NavigableMap

//这里的m是无参构造TreeSet时构造的一个TreeMap对象,所以put方法也是TreeMap中的方法。

public boolean add(E e) {

return m.put(e, PRESENT)==null;

}

}

public class TreeMap implements NavigableMap{

//如果是无参构造,也就是自然排序,则comparator排序器为null

public TreeMap() {

comparator = null;

}

public V put(K key, V value) {

//生成一个根

Entry t = root;

//在还没有元素插入的时候,树的根是null,然后生成一个根

if (t == null) {

compare(key, key);

root = new Entry<>(key, value, null);

size = 1;

modCount++;

return null;

}

int cmp;

Entry parent;

//由于是无参构造,comparator的值是null

Comparator 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 {

//key就是想要添加到集合中的元素值

if (key == null)

throw new NullPointerExcepti

【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

on();

@SuppressWarnings(“unchecked”)

//这里进行了强制类型转换,向下转型

//Comparable是自然比较

//这里要求了传入的对象的类必须实现了 Comparable接口

Comparable k = (Comparable) 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 e = new Entry<>(key, value, parent);

if (cmp < 0)

parent.left = e;

else

parent.right = e;

fixAfterInsertion(e);

size++;

modCount++;

return null;

}

}

这里具体的过程可以拿第一个例子,传入的对象是Integer类型的例子来说明

[](

)自定义类继承Comparable接口

现在回过头来,为什么Integer可以实现自然排序,因为Integer类已经实现Comparable接口

那么如果想让自定义类的对象也可以实现自然排序,就需要让自定义类也实现Comparable接口

package review.SetDemo;

public class Student2 implements Comparable{

private String name;

private int age;

public Student2() {

}

public Student2(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 int compareTo(Student2 o) {

int i = this.age - o.age;

//先比较年龄是否相同,如果不同则返回年龄的比较结果

//如果年龄相同则比较姓名是否相同

//姓名相同则返回0,说明两个元素相同

//姓名不同则按照this.name.compareTo(o.name)返回的结果排序

int i2 = i==0 ? this.name.compareTo(o.name) : i;

return i2;

}

}

package review.SetDemo;

import java.util.TreeSet;

public class demo4 {

public static void main(String[] args) {

TreeSet tree = new TreeSet<>();

Student2 s1 = new Student2(“zhang”,12);

Student2 s2 = new Student2(“zhou”,15);

Student2 s3 = new Student2(“cao”,16);

Student2 s4 = new Student2(“zhang”,23);

Student2 s5 = new Student2(“zhang”,12);

Student2 s6 = new Student2(“jing”,15);

tree.add(s1);

tree.add(s2);

tree.add(s3);

tree.add(s4);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号