Java笔试常用接口:Comparator接口实现自定义对象的数组排序

版权声明:如需转载注明出处 https://blog.csdn.net/u011463794/article/details/89285145

在日常写程序时,或做笔试题时总会遇到自己定义的类或者数据结构,如果把自己定义的类存入List这样的数组中,排序是很麻烦的,比如:
下面定义了一个Person类

class Person{
	String name;
	int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

如果我们要对存了很多Person的数组排序的话,首先要确定排序规定,就是按什么排序,比如按照年龄大小,按照名字之类的,如果我们采用常规方法取值在排序然后交换位置时很麻烦的,好在List为我们提供了sort方法,下面是jdk文档
在这里插入图片描述

Parameters:
c - the Comparator used to compare list elements. A null value indicates that the elements’ natural ordering should be used

依旧是List的sort方法根据特定的Comparator排序。使用list.sort(c )的形式排序,c就是这个特定的Comparator对象。Comparator就是我们比较的准则。下面看啊可能Comparator类:

@FunctionalInterface
public interface Comparator< T>
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have a natural ordering.

这是一个函数式接口,用于比较两个对象,因为是函数式接口,所以我们可以采用Lambda表达式去实现这个接口,如果不知道什么是函数式接口或者有兴趣用Lambda表达式,参考下面这篇博客:Lambda表达式

下面介绍一下如何实现这个接口:

上面是实现接口必须实现的方法,所以我们写一个类实现这个接口,并且实现这个方法:

class PersonSortByAge implements Comparator<Person>{
	@Override
	public int compare(Person o1, Person o2) {
		return o1.age-o2.age;
	}
}

这个就很好理解

  • 如果返回值等于零:o1=o2
  • 返回值大于零则o1>o2
  • 返回值小于于零则o1<o2

然后就可以在集合中排序了:

public class ComparatorTest {

	public static void main(String[] args) {

		List<Person> personList = new ArrayList<Person>();
		personList.add(new Person("ace",22));
		personList.add(new Person("xb",21));
		personList.add(new Person("glm",36));
		personList.add(new Person("sxy",20));
		personList.sort(new PersonSortByAge());
		for(Person p:personList)
			System.out.println(p);
		
	}
}

结果如下,按照年龄大小排好了。
在这里插入图片描述

如果想试试别的排序方法,下面我给出例子:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* 项目名:		CSDN
* 包名:		Compare
* 文件名:		ComparatorTest.java
* 创建时间:	2019年4月13日
* 
* @author:	xiatom
* 描述:		比较方法——comparator
* 
*
**/

class PersonSortByAgeSmall implements Comparator<Person>{

	@Override
	public int compare(Person o1, Person o2) {
		return o1.age-o2.age;
	}
	
}

class PersonSortByAgeBig implements Comparator<Person>{

	@Override
	public int compare(Person o1, Person o2) {
		return o2.age-o1.age;
	}
	
}

class PersonSortByName implements Comparator<Person>{

	@Override
	public int compare(Person o1, Person o2) {
		return o1.name.compareTo(o2.name);
	}
	
}


class Person{
	String name;
	int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
}

public class ComparatorTest {

	public static void main(String[] args) {

		List<Person> personList = new ArrayList<Person>();
		personList.add(new Person("ace",22));
		personList.add(new Person("xb",21));
		personList.add(new Person("glm",36));
		personList.add(new Person("sxy",20));
		System.out.println("按照年龄,大的排前面");
		personList.sort(new PersonSortByAgeBig());
		for(Person p:personList)
			System.out.println(p);
		System.out.println();

		System.out.println("按照年龄,小的排前面");
		personList.sort(new PersonSortByAgeSmall());
		for(Person p:personList)
			System.out.println(p);
		

		System.out.println();
		System.out.println("按照名字排序");
		personList.sort(new PersonSortByName());
		for(Person p:personList)
			System.out.println(p);
		
	}
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u011463794/article/details/89285145
今日推荐