Java进阶篇9——TreeSet

1. 集合类总纲

在这里插入图片描述

2. TreeSet

2.1 TreeSet 性质

  • TreeSet 是一个有序的Set 集合,可以按一定的规则指定元素的顺序。
  • 不能有重复元素。
  • 对象重写 equals() 和 hashcode() 失效。
  • 无序不重复

2.2 元素有序

2.2.1 使用默认排序

package javaweb.collection;

import java.util.Iterator;
import java.util.TreeSet;

public class MyTreeSet {
	public static void practice1() {
		TreeSet<String> treeset = new TreeSet<String>();
		
		treeset.add("a");
		treeset.add("c");
		treeset.add("e");
		treeset.add("f");
		treeset.add("b");
		
		Iterator<String> it = treeset.iterator();
		while(it.hasNext()) {
			System.out.print(it.next() + " ");
		}
	}

	public static void main(String[] args) {
		practice1();
	}

}
a b c e f 

2.2.2 自定义排序

2.2.2.1 自己构造比较器
package javaweb.collection;

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

/**
 * 自定义排序,降序
 *
 * @author NoBug
 * @version 1.0
 * @time 2022/3/16
 * @Blog https://blog.youkuaiyun.com/qq_51184516
 *
 */
class MyCmp implements Comparator<Object> {

	@Override
	public int compare(Object obj1, Object obj2) {
		int x = 0;
		Student s1 = (Student) obj1;
		Student s2 = (Student) obj2;

		if (s1.getAge() > s2.getAge())
			x = -1;
		else if (s1.getAge() < s2.getAge())
			x = 1;
		else
			x = s1.getId().compareTo(s2.getId()); // 前者大于后者,返回1

		return x;
	}
}

public class MyTreeSet {

	public static void main(String[] args) {
		TreeSet<Student> treeset = new TreeSet<Student>(new MyCmp());

		Student s1 = new Student("1001", "chen", 18);
		Student s2 = new Student("1001", "luo", 25);
		Student s3 = new Student("1003", "zhang", 25);
		Student s4 = new Student("1004", "pi", 20);
		Student s5 = new Student("1002", "wang", 25);

		treeset.add(s1);
		treeset.add(s2);
		treeset.add(s3);
		treeset.add(s4);
		treeset.add(s5);

		Iterator<Student> it = treeset.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}

}

按年龄降序,相同年龄,按学号升序

1001 luo 25
1002 wang 25
1003 zhang 25
1004 pi 20
1001 chen 18

附件:Studen类

package javaweb.collection;

import java.util.HashSet;
import java.util.Iterator;

/**
 * 测试不重复
 *
 * @author NoBug
 * @version 1.0
 * @time 2022/3/16
 * @Blog https://blog.youkuaiyun.com/qq_51184516
 *
 */
public class Student {
	String id, name;
	int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Student(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return id + " " + name + " " + age;
	}

	@Override
	public boolean equals(Object obj) {
		return id.equals(((Student) obj).id);
	}

	@Override
	public int hashCode() {
		return Integer.valueOf(id);
	}

	public static void main(String[] args) {
		HashSet<Student> hashset = new HashSet<Student>();

		Student student1 = new Student("1001", "chen", 18);
		Student student2 = new Student("1001", "chen", 18);
		hashset.add(student1);
		hashset.add(student2);

		Iterator<Student> it = hashset.iterator();
		while (it.hasNext()) {
			System.out.println(it.next() + " ");
		}
	}

}
2.2.2.2 在 Student类里面就重写 compareTo()
package javaweb.collection;

import java.util.Iterator;
import java.util.TreeSet;

public class Student1 implements Comparable<Object> {
	String id, name;
	int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Student1(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return id + " " + name + " " + age;
	}

	@Override
	public int compareTo(Object o) {
		int x = 0;
		Student1 s2 = (Student1) o;

		if (getAge() > s2.getAge())
			x = -1;
		else if (getAge() < s2.getAge())
			x = 1;
		else
			x = getId().compareTo(s2.getId()); // 前者大于后者,返回1

		return x;
	}

	public static void main(String[] args) {
		TreeSet<Student1> treeset = new TreeSet<Student1>();

		Student1 s1 = new Student1("1001", "chen", 18);
		Student1 s2 = new Student1("1001", "luo", 25);
		Student1 s3 = new Student1("1003", "zhang", 25);
		Student1 s4 = new Student1("1004", "pi", 20);
		Student1 s5 = new Student1("1002", "wang", 25);

		treeset.add(s1);
		treeset.add(s2);
		treeset.add(s3);
		treeset.add(s4);
		treeset.add(s5);

		Iterator<Student1> it = treeset.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
1001 luo 25
1002 wang 25
1003 zhang 25
1004 pi 20
1001 chen 18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜满月

鼓励,鼓励,更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值