java learning: a container sorting: TreeMap and TreeSet implement custom sorting to use? Difference between the two?

TreeMap and TreeSet implement custom sorting implementation

1, TreeMap implement custom sorting

(1) the constructor new Comparator, anonymous inner classes, override the compare method.
(2) the entity class implements Comparable, override compareTo method.

(1) the constructor new Comparator, anonymous inner classes, override the compare method.

Entity classes:

public class Person {
	private final String name;
	private final int handsome;//帅气值
	public String getName() {
		return name;
	}
	public int getHandsome() {
		return handsome;
	}
	public Person(String name, int handsome) {
		super();
		this.name = name;
		this.handsome = handsome;
	}
	public Person() {
			name=null;
			handsome=0;
	}
	@Override
	public String toString() {
		return "名字:" + name + ", 帅气值: " + handsome + "\n";
	}
}

Custom Sort:

public class TreeMapDemo {

	public static void main(String[] args) {

		Person p1 = new Person("刘德华", 100);
		Person p2 = new Person("赵小花", 30);
		Person p3 = new Person("王大华", 90);
		Person p4 = new Person("邓小二", 600);
		Person p5 = new Person("杨柳", 50);

		// 使用排序的业务类(匿名内部类)
		TreeMap<Person, String> map = new TreeMap<Person, String>(
				new Comparator<Person>() {
					@Override
					public int compare(Person o1, Person o2) {
						return o1.getHandsome() - o2.getHandsome();//帅气值升序
					}
				});
		map.put(p1, "ll");
		map.put(p2, "ll");
		map.put(p3, "ll");
		map.put(p4, "ll");
		map.put(p5, "ll");
		
		for(Person per:map.keySet()){
			System.out.println(per.getName()+"的帅气值:   "+per.getHandsome());
		}

//		// 查看键
//		Set<Person> persons = map.keySet();
//
//		System.out.println(persons);

	}
}

operation result:Here Insert Picture Description

(2) the entity class implements Comparable, override compareTo method.

Entity classes: interfaces to achieve comparable rewrite compareto method.

public class Worker implements Comparable<Worker>{
	private String type;//工种
	
	private double salary;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Worker(String type, double salary) {
		super();
		this.type = type;
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "工种:" + type + ",工资:" + salary + "\n";
	}

	public Worker() {
		}
	
	public int compareTo(Worker o){//工资升序
		return (this.getSalary()-o.getSalary())>0?1:(this.getSalary()-o.getSalary())==0?0:-1;
		
	}

}

application:

public class TreeMapDemo02 {
	public static void main(String[] args) {

		Worker w1 = new Worker("回收员", 12000);
		Worker w2 = new Worker("思想者", 60000);
		Worker w3 = new Worker("农民工", 30000);
		Worker w4 = new Worker("打字员", 5000);
		Worker w5 = new Worker("售货员", 10000);

		// 使用排序的实体类实现
		TreeMap<Worker, String> map = new TreeMap<>();
		map.put(w1, "ll");
		map.put(w2, "ll");
		map.put(w3, "ll");
		map.put(w4, "ll");
		map.put(w5, "ll");

		for(Worker per:map.keySet()){
			System.out.println(per.getType()+"的工资:   "+per.getSalary());
		}
	}
}

operation result:
Here Insert Picture Description

2, TreeSet implement custom sorting

Exactly the same TreeMap.
(1) the constructor new Comparator, anonymous inner classes, override the compare method.
(2) the entity class implements Comparable, override compareTo method.

(1) the constructor new Comparator, anonymous inner classes, override the compare method.

public class TreeSetDemo {

	public static void main(String[] args) {

		Person p1 = new Person("刘德华", 100);
		Person p2 = new Person("赵小花", 30);
		Person p3 = new Person("王大华", 90);
		Person p4 = new Person("邓小二", 600);
		Person p5 = new Person("杨柳", 50);

		// 使用排序的业务类(匿名内部类)
		TreeSet<Person> persons = new TreeSet<Person>(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return -(o1.getHandsome() - o2.getHandsome());//降序
			}
		});
		//treeset在添加数据的时候,就进行排序了。
		//数据更改不会影响原来的顺序。
		//treeset在使用时,不要修改数据,否则可能会造成数据重复。
		//可以用final修饰原对象
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		persons.add(p4);
		persons.add(p5);
		
		for(Person per:persons){
			System.out.println(per.getName()+"  帅气分数:  "+per.getHandsome());
		}

	}
}

(2) the entity class implements Comparable, override compareTo method.

public class TreeSetDemo02 {

	public static void main(String[] args) {
		Worker w1 = new Worker("回收员", 12000);
		Worker w2 = new Worker("思想者", 60000);
		Worker w3 = new Worker("农民工", 30000);
		Worker w4 = new Worker("打字员", 5000);
		Worker w5 = new Worker("售货员", 10000);

		// 使用排序的实体类实现
		TreeSet<Worker> workers = new TreeSet<>();
		workers.add(w1);
		workers.add(w2);
		workers.add(w3);
		workers.add(w4);
		workers.add(w5);

		System.out.println(workers);

	}

(3) It should be noted TreeSet and TreeMap differences

TreeSet when adding data, it sort of. So change the data after the operation will not affect the original order.
So when using TreeSet, you need to be careful not to modify the data, otherwise it will cause data errors.
Of course, we can use the final modification of the original object.

Published 57 original articles · won praise 13 · views 1117

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105179696