Comparable 和 Comparator 接口 的 使用

1. Comparable接口的使用:自定义类(Student)实现(implements)Comparable接口,重写compareTo方法,从compareTo方法中规定比较器比较的规则。

代码如下: 

Student.java:

package com.demo.Main;

import java.util.Comparator;

public class Student implements Comparable<Student>{
	private String name ;
	private int age ;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = 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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	@Override
	public int compareTo(Student stu) {
		//先比较名字长度,如果名字长度一样,再比较名字的内容,名字内容一样了,再比较年龄。
		int lenght = this.getName().length() - stu.getName().length() ; 
		int number = lenght == 0 ? this.getName().compareTo(stu.getName()): lenght ;// this.getName().compareTo(stu.getName())调用的是String中的CompareTo()方法。
		return number == 0 ? this.getAge() - stu.getAge() : number ;  
	} 
	
}

Main.java:

package com.demo.Main;

import java.util.TreeSet;

public class Main {

	public static void main(String[] args) {

		TreeSet<Student> ts = new TreeSet<>() ; 
		ts.add(new Student("李四",14));
		ts.add(new Student("张三",13));
		ts.add(new Student("陈七",17));
		ts.add(new Student("王五",15));
		ts.add(new Student("赵六",16));
		System.out.println(ts);
		//[Student [name=张三, age=13], Student [name=李四, age=14], Student [name=王五, age=15], Student [name=赵六, age=16], Student [name=陈七, age=17]]
	}

}

2.Comparator 接口 的使用:

给TreeSet构造器传入一个Comparator接口的实现类,在实现类中 重写 compare(T o1 , T o2)方法。

注意: 传入的这个comparator接口的实现类,可以  直接定义一个实现类 ,也可以用匿名内部类。

Student.java:

package com.demo.Main;

import java.util.Comparator;

public class Student{
	private String name ;
	private int age ;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = 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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
}

(1)直接外部定义一个实现类传入构造器中,实现根据比较器的规则进行比较:

package com.demo.Main;

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

public class Main {

	public static void main(String[] args) {

		
		TreeSet<Student> ts = new TreeSet<>(new getNewComparator()) ; 
		ts.add(new Student("李四",14));
		ts.add(new Student("张三",13));
		ts.add(new Student("陈七",17));
		ts.add(new Student("王五",15));
		ts.add(new Student("赵六",16));
		System.out.println(ts);
		
	}
}
class getNewComparator implements Comparator<Student>{

	@Override
	public int compare(Student o1, Student o2) {
		//先根据名字长度比较,如果相同再比较名字内容,名字相同,再比较年龄。
		int lenght = o1.getName().length() - o2.getName().length() ;
		int number = lenght == 0 ? o1.getName().compareTo(o2.getName()) : lenght ;
		return number == 0 ? o1.getAge() - o2.getAge() : number ;
	}
	
}

(2)用匿名内部类,传入Comparator对象。

package com.demo.Main;

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

public class Main {

	public static void main(String[] args) {

		
		TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				//先根据名字长度比较,如果相同再比较名字内容,名字相同,再比较年龄。
				int lenght = o1.getName().length() - o2.getName().length() ;
				int number = lenght == 0 ? o1.getName().compareTo(o2.getName()) : lenght ;
				return number == 0 ? o1.getAge() - o2.getAge() : number ;
			}
		}) ; 
		ts.add(new Student("李四",14));
		ts.add(new Student("张三",13));
		ts.add(new Student("陈七",17));
		ts.add(new Student("王五",15));
		ts.add(new Student("赵六",16));
		System.out.println(ts);
		
	}
}

以上就是 comparable 和 comparator 接口 中 compareTo 和 compare  比较方法 的 使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值