TreeSet集合添加自定义类的定制排序实例

如果自定义的类想要添加到TreeSet集合中就必须 实现 Comparable 或者 Comparator


如果实现Comparator  就是  定制排序


定制排序的步骤

1 定义bean类
2 创建一个类 去实现Comparator接口  并且重写compare方法

3 创建TreeSet对象  向构造方法中传递

public class TestNO2Student {
	private int age;
	private String Bname;
	private String name;
	private int price;

	public TestNO2Student(int age, String bname, String name, int price) {
		super();
		this.age = age;
		Bname = bname;
		this.name = name;
		this.price = price;
	}

	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getBname() {
		return Bname;
	}

	public void setBname(String bname) {
		Bname = bname;
	}

	@Override
	public String toString() {
		return "TestNO2Student [Bname=" + Bname + ", age=" + age + ", name="
				+ name + ", price=" + price + "]";
	}

}

public class TestNO2Comp implements Comparator<TestNO2Student> {

	@Override
	public int compare(TestNO2Student o1, TestNO2Student o2) {
		if (o1.getAge() > o2.getAge()) {
			return 1;
		} else if (o1.getAge() < o2.getAge()) {
			return 2;
		} else {
			CollationKey key = Collator.getInstance().getCollationKey(
					o1.getName());
			CollationKey key1 = Collator.getInstance().getCollationKey(
					o2.getName());
			if (key.compareTo(key1) > 0) {
				return 1;
			} else if (key.compareTo(key1) < 0) {
				return -1;
			} else {
				if (o1.getPrice() > o2.getPrice()) {
					return -1;
				} else if (o1.getPrice() < o2.getPrice()) {
					return 1;
				} else {
					CollationKey key3 = Collator.getInstance().getCollationKey(
							o1.getBname());
					CollationKey key4 = Collator.getInstance().getCollationKey(
							o2.getBname());
					return key3.compareTo(key4);
				}
			}
		}
	}
}

public class TestNo2 {
	public static void main(String[] args) {
		TreeSet<TestNO2Student> set = new TreeSet<TestNO2Student>(
				new TestNO2Comp());
		set.add(new TestNO2Student(11, "童年", "蒋", 22));
		set.add(new TestNO2Student(13, "在人间", "蒋", 2));
		set.add(new TestNO2Student(11, "我的大学", "刘", 22));
		set.add(new TestNO2Student(21, "童年", "杨", 22));
		System.out.println(set);
		// 或者使用匿名类
		TreeSet<TestNO2Student> set1 = new TreeSet<TestNO2Student>(
				new Comparator<TestNO2Student>() {

					@Override
					public int compare(TestNO2Student o1, TestNO2Student o2) {
						if (o1.getAge() > o2.getAge()) {
							return 1;
						} else if (o1.getAge() < o2.getAge()) {
							return 2;
						} else {
							CollationKey key = Collator.getInstance()
									.getCollationKey(o1.getName());
							CollationKey key1 = Collator.getInstance()
									.getCollationKey(o2.getName());
							if (key.compareTo(key1) > 0) {
								return 1;
							} else if (key.compareTo(key1) < 0) {
								return -1;
							} else {
								if (o1.getPrice() > o2.getPrice()) {
									return -1;
								} else if (o1.getPrice() < o2.getPrice()) {
									return 1;
								} else {
									CollationKey key3 = Collator.getInstance()
											.getCollationKey(o1.getBname());
									CollationKey key4 = Collator.getInstance()
											.getCollationKey(o2.getBname());
									return key3.compareTo(key4);
								}
							}
						}
					}
				});
		set1.add(new TestNO2Student(11, "童年", "张", 22));
		set1.add(new TestNO2Student(13, "在人间", "蒋", 26));
		set1.add(new TestNO2Student(11, "我的大学", "", 22));
		set1.add(new TestNO2Student(21, "童年", "蒋", 22));
		System.out.println(set1);

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值