自定义比较器

目录

1.自定义类排序

2.优先级队列小根堆改为大根堆


1.自定义类排序

问题描述:

        我们在使用排序的过程中会发现,系统中的基本类型是直接可以使用Arrays.sort方法排序的,但是我们自定义的类型用这个方法却失效了,这是因为我们并没有给我们自定义的方法一个比较的准则,导致使用Arrays.sort方法排序时系统不知道如何排序。

        此时我们就可以自定义一个比较器,如下面代码:

public class ShowComparator {

	public static class Student {
		public String name;
		public int id;
		public int age;

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

	// 谁id大,谁放前!
	public static class IdComparator implements Comparator<Student> {

		// 如果返回负数,认为第一个参数应该排在前面
		// 如果返回正数,认为第二个参数应该排在前面
		// 如果返回0,认为谁放前面无所谓
		@Override
		public int compare(Student o1, Student o2) {
			if (o1.id < o2.id) {
				return 1;
			} else if (o2.id < o1.id) {
				return -1;
			} else {
				return 0;
			}
		}
	}
	
	// 谁age大,谁放前!
	public static class AgeComparator implements Comparator<Student> {

		// 如果返回负数,认为第一个参数应该排在前面
		// 如果返回正数,认为第二个参数应该排在前面
		// 如果返回0,认为谁放前面无所谓
		@Override
		public int compare(Student o1, Student o2) {
			if (o1.age < o2.age) {
				return 1;
			} else if (o2.age < o1.age) {
				return -1;
			} else {
				return 0;
			}
		}
	}

	public static void printArray(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}

	public static void printStudents(Student[] students) {
		for (int i = 0; i < students.length; i++) {
			System.out.println(students[i].name + ", " + students[i].id + ", " + students[i].age);
		}
	}

	public static void main(String[] args) {
		int[] arr = { 8, 1, 4, 1, 6, 8, 4, 1, 5, 8, 2, 3, 0 };
		printArray(arr);
		Arrays.sort(arr);
		printArray(arr);

		Student s1 = new Student("张三", 5, 27);
		Student s2 = new Student("李四", 1, 17);
		Student s3 = new Student("王五", 4, 29);
		Student s4 = new Student("赵六", 3, 9);
		Student s5 = new Student("左七", 2, 34);

		Student[] students = { s1, s2, s3, s4, s5 };
		printStudents(students);
		System.out.println("=======");
		Arrays.sort(students, new IdComparator());
		printStudents(students);
		System.out.println("=======");

		ArrayList<Student> arrList = new ArrayList<>();
		arrList.add(s1);
		arrList.add(s2);
		arrList.add(s3);
		arrList.add(s4);
		arrList.add(s5);
		for (Student s : arrList) {
			System.out.println(s.name + ", " + s.id + ", " + s.age);
		}
		System.out.println("=======");
		arrList.sort(new AgeComparator());
		for (Student s : arrList) {
			System.out.println(s.name + ", " + s.id + ", " + s.age);
		}

	}

}

2.优先级队列小根堆改为大根堆

        优先级队列底层默认是小根堆,我们在每次弹出元素时一定是最小的而且此时间复杂度为logN级别。此时我们的需求是把优先级队列底层的小根堆修改为大根堆,编写我们的自定义比较器可以实现。

import java.util.Comparator;
import java.util.PriorityQueue;

public class ShowComparator2 {

	public static class MyComparator implements Comparator<Integer> {

		// 负,第一个参数在前
		// 正,第二个参数在前
		// 0, 谁放前都行
		@Override
		public int compare(Integer o1, Integer o2) {
			if (o1 < o2) {
				return 1;
			} else if (o1 > o2) {
				return -1;
			} else {
				return 0;
			}
		}

	}

	public static class Student {
		public String name;
		public int id;
		public int age;

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

	// 谁id大,谁放前!
	public static class IdComparator implements Comparator<Student> {

		// 如果返回负数,认为第一个参数应该排在前面
		// 如果返回正数,认为第二个参数应该排在前面
		// 如果返回0,认为谁放前面无所谓
		@Override
		public int compare(Student o1, Student o2) {
			if (o1.id < o2.id) {
				return 1;
			} else if (o2.id < o1.id) {
				return -1;
			} else {
				return 0;
			}
		}
	}

	public static void main(String[] args) {
		String str1 = "abc";
		String str2 = "b";
		System.out.println(str1.compareTo(str2));
		PriorityQueue<Student> heap = new PriorityQueue<>(new IdComparator());
		Student s1 = new Student("张三", 5, 27);
		Student s2 = new Student("李四", 1, 17);
		Student s3 = new Student("王五", 4, 29);
		Student s4 = new Student("赵六", 3, 9);
		Student s5 = new Student("左七", 2, 34);
		heap.add(s1);
		heap.add(s2);
		heap.add(s3);
		heap.add(s4);
		heap.add(s5);
		System.out.println("=========");
		while (!heap.isEmpty()) {
			Student s = heap.poll();
			System.out.println(s.name + ", " + s.id + ", " + s.age);
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值