对象比较器

对象比较器

1.先说Arrays.sort快排常见

  1. 比较数字从小到大
    <span style="font-size:18px;">		int[] nums = {12,32,44,5,4,65};
    		Arrays.sort(nums);
    		for (int i : nums)
    		{
    			System.out.println(i);
    		}</span>

  2. sort也可以用来排列字符型数组(按字典序排序)
    		String[] names = {"abc","vince","java","com","tom"};
    		Arrays.sort(names);
    		for (String string : names){
    			System.out.println(string);
    		}
2.如果要用sort比较自己定义的类


  1. 比如用自己定义的类Cat     Arrays.sort(Cat);报错!
产生错误的原因差api知
Comparable没有Cat自定义类实现

代码实现:
所以Cat函数要这样写 用comparato函数比较大于返回1小于返回-1等于返回0;


<span style="font-size:18px;">package com.vince;

public class Cat implements Comparable<Cat>{
	// 告诉其所要对比的对象
	private String name;
	private int age;	
	
	//应用快捷键shift+Alt+s或 鼠标右键-->源码
	public Cat() {}
	public Cat(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 "Cat [name=" + name + ", age=" + age + "]";
	}
	<span style="color:#ff0000;">//通过此方法实现对象的比较
	@Override
	public int compareTo(Cat o) {
		if(this.age<o.age){
			return -1;
		}
		else if (this.age>o.age){
			return 1;
		}
		else{
			return 0;
		}
	}</span>
}</span>
然后可以直接使用Arrays.sort(Cat)排序
//类不能被sort的接收(Cat 不能转化为Comparable接口)
		Cat[] cats = {new Cat("Tom",2),new Cat("GG",4),new Cat("Wg",3),
				new Cat("Bi",8),new Cat("DD",5)};
		Arrays.sort(cats);
		for (Cat cat : cats) {
			System.out.println(cat);
		}
输出结果



自定义比较类型用comparator(比较器)
假如类已经定义好了,不能再去修改它:
<span style="font-size:18px;">package com.vince;

public class Dog {
	private String name;
	private int age;
	public Dog(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 "Dog [name=" + name + ", age=" + age + "]";
	}
	
	
}</span>

没有定义比较端口要实现类比较

Comparator可以比较灵活的比较对象中的属性

<span style="font-size:18px;">package com.vince;

import java.util.Comparator;

public class DogComparator implements Comparator<Dog>{

	@Override
	public int compare(Dog o1, Dog o2) {
		if (o1.getAge()<o2.getAge()){
			return -1;
		}
		else if (o1.getAge()>o2.getAge()){
			return 1;
		}
		return 0;
	}
	</span>
}


所以要用sort比较时要告诉sort函数比较器

<span style="font-size:18px;">		//类不能被sort的接收(Dog 重新写comparator写比较的属性)
		Dog[] dogs = {new Dog("Tom",2),new Dog("GG",4),new Dog("Wg",3),
				new Dog("Bi",8),new Dog("DD",5)};
		Arrays.sort(dogs,new DogComparator());//比较类内的属性用重写DogComparable
		for (Dog dog : dogs) {
			System.out.println(dog);
		}</span>

输出结果和Cat一样


public class Comparators { public static java.util.Comparator getComparator() { return new java.util.Comparator() { public int compare(Object o1, Object o2) { if (o1 instanceof String) { return compare( (String) o1, (String) o2); } else if (o1 instanceof Integer) { return compare( (Integer) o1, (Integer) o2); } else if (o1 instanceof Person) { return compare( (Person) o1, (Person) o2); } else { System.err.println("未找到合适的比较器"); return 1; } } public int compare(String o1, String o2) { String s1 = (String) o1; String s2 = (String) o2; int len1 = s1.length(); int len2 = s2.length(); int n = Math.min(len1, len2); char v1[] = s1.toCharArray(); char v2[] = s2.toCharArray(); int pos = 0; while (n-- != 0) { char c1 = v1[pos]; char c2 = v2[pos]; if (c1 != c2) { return c1 - c2; } pos++; } return len1 - len2; } public int compare(Integer o1, Integer o2) { int val1 = o1.intValue(); int val2 = o2.intValue(); return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1)); } public int compare(Boolean o1, Boolean o2) { return (o1.equals(o2)? 0 : (o1.booleanValue()==true?1:-1)); } public int compare(Person o1, Person o2) { String firstname1 = o1.getFirstName(); String firstname2 = o2.getFirstName(); String lastname1 = o1.getLastName(); String lastname2 = o2.getLastName(); Boolean sex1 = o1.getSex(); Boolean sex2 = o2.getSex(); Integer age1 = o1.getAge(); Integer age2 = o2.getAge(); return (compare(firstname1, firstname2) == 0 ? (compare(lastname1, lastname2) == 0 ? (compare(sex1, sex2) == 0 ? (compare(age1, age2) == 0 ? 0 : compare(age1, age2)) : compare(sex1, sex2)) : compare(lastname1, lastname2)) : compare(firstname1, firstname2)); } }; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值