java.util 集合TreeSet排序的三种方法

public class TreeSet<E>

  extends AbstractSet<E>

implements NavigableSet<E>, Cloneable, Serializable

基于 TreeMapNavigableSet 实现。使用元素的自然顺序对元素进行排序,或者根据创建 set 时提供的 Comparator 进行排序,具体取决于使用的构造方法。

//      迭代其实我们可以简单地理解为遍历,是一个标准化遍历各类容器里面的所有对象的方法类,
//		它是一个很典型的设计模式。Iterator模式是用于遍历集合类的标准访问方法。
//		它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。 

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

public class TreeSetDemo {
	/**
	 * 放入的字符串,按照字符串的自然顺序排序,字符串的自然顺序就是字典顺序
	 */
	public static void testMethod() {
		TreeSet treeset = new TreeSet();
		treeset.add("was");
		treeset.add("db2");
		treeset.add("java");
		// boolean add(E e) 将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。
		Iterator iterator = treeset.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}

	/**
	 * 向TreeSet放入自定义对象 自定义类要实现Comparable接口
	 */
	public static void testMethod2() {
		TreeSet treeset = new TreeSet();
		Employee one = new Employee("zs", 22);
		Employee two = new Employee("ww", 18);
		Employee three = new Employee("lisi", 26);
		treeset.add(one);
		treeset.add(two);
		treeset.add(three);

		Iterator iterator = treeset.iterator();
		while (iterator.hasNext()) {
			Employee emp = (Employee) iterator.next();
			System.out.println(emp.getName() + "==" + emp.getAge());
		}
	}

	/**
	 * 向TreeSet放入自定义对象 产生集合时给其传递排序规则
	 */
	public static void testMethod3() {
		TreeSet set = new TreeSet(new ComparatorDemo());
		Employee one = new Employee("zs", 22);
		Employee two = new Employee("ww", 18);
		Employee three = new Employee("lisi", 26);
		set.add(one);
		set.add(two);
		set.add(three);
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			Employee emp = (Employee) iterator.next();
			System.out.println(emp.getName() + "==" + emp.getAge());
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		testMethod2();


	}

}
public class Employee implements Comparable {
	private String name;
	private int age;

	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Employee emp = (Employee) o;
		return emp.age - this.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;
	}

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String toString() {
		return this.name + "==" + this.age;
	}

}
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;


public class ComparatorDemo implements Comparator {

//	public int compare(Object o1, Object o2) {
//		// TODO Auto-generated method stub
//		Employee one =(Employee)o1;
//		Employee two =(Employee)o2;
//		return one.getAge()-two.getAge();
//	}
	
	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Comparator reversed() {
		// TODO Auto-generated method stub
		return null;
	}

	public Comparator thenComparing(Comparator other) {
		// TODO Auto-generated method stub
		return null;
	}

	public Comparator thenComparing(Function keyExtractor,
			Comparator keyComparator) {
		// TODO Auto-generated method stub
		return null;
	}

	public Comparator thenComparing(Function keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public Comparator thenComparingInt(ToIntFunction keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public Comparator thenComparingLong(ToLongFunction keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public Comparator thenComparingDouble(ToDoubleFunction keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T, U> Comparator<T> comparing(
			Function<? super T, ? extends U> keyExtractor,
			Comparator<? super U> keyComparator) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
			Function<? super T, ? extends U> keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T> Comparator<T> comparingInt(
			ToIntFunction<? super T> keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T> Comparator<T> comparingLong(
			ToLongFunction<? super T> keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}

	public static <T> Comparator<T> comparingDouble(
			ToDoubleFunction<? super T> keyExtractor) {
		// TODO Auto-generated method stub
		return null;
	}



	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

itzixiao

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值