java比较器以及集合类实现排序

一、java实现比较有两种方式,分别是实现Comparable接口与实现Comparator接口两种方式

方式一:

实现Comparable 接口

package com.loan.entity;

import lombok.Data;

@Data
public class Test implements Comparable{
private int id;
private String name;
private int age;
@Override
public int compareTo(Test o) {
// TODO Auto-generated method stub
return this.name.compareTo(o.getName());
}
public Test() {
}
public Test(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Test test1=new Test(1,"aser1",20);
Test test2=new Test(2,"aser1",30);
int result=test1.compareTo(test2);
String res=result>0?"test1>test2":"test2>test1";
System.out.println(res);
}
}

方式二:实现Comparator接口,实现者被称为比较器,不同于Comparable的在于Comparator接口一般不会被集合元素类所实现,而是单独实现或者用匿名内部类方式实现

1、单独实现:

package com.loan.entity;

import java.util.Comparator;

import lombok.Data;

@Data
public class Test{
private int id;
private String name;
private int age;
public Test(){}
public Test(int id, String name, int age) {
	this.id = id;
	this.name = name;
	this.age = age;
}
public static void main(String[] args) {
	Test test1=new Test(1,"aser1",20);
	Test test2=new Test(2,"aser1",30);
	//static访问成员内部类,必须先实例化外部类对象
	Comparator comp=new Test().new TestComparator();
	int result=comp.compare(test1,test2);
	System.out.println(result>0?"tes1>test2":"test1{
	@Override
	public int compare(Test o1, Test o2) {
		// TODO Auto-generated method stub
		return o1.getAge()-o2.getAge();
	}
	
}
}

匿名内部类实现:

package com.loan.entity;

import java.util.Comparator;

import lombok.Data;

@Data
public class Test{
private int id;
private String name;
private int age;
public Test(int id, String name, int age) {
	this.id = id;
	this.name = name;
	this.age = age;
}
public static void main(String[] args) {
	Test test1=new Test(1,"aser1",20);
	Test test2=new Test(2,"aser1",30);
	int result=new Comparator(){


@Override
public int compare(Test o1, Test o2) {
// TODO Auto-generated method stub
return o1.getAge()-o2.getAge();
}
}.compare(test1, test2);
	System.out.println(result>0?"test1>test2":"test1

二、集合类实现排序

package com.loan.entity;

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

import lombok.Data;

@Data
public class Test implements Comparable{
private int id;
private String name;
private int age;
public Test(){}
public Test(int id, String name, int age) {
	this.id = id;
	this.name = name;
	this.age = age;
}
public static void main(String[] args) {
	Test test1=new Test(1,"user1",20);
	Test test2=new Test(2,"user2",10);
	Test test3=new Test(3,"user3",40);
	Test test4=new Test(4,"user4",30);
	List list=new ArrayList();
	list.add(test1);
	list.add(test2);
	list.add(test3);
	list.add(test4);
	String s="原来顺序:";
	for(Test test:list){
		s+=test.getName()+"_"+test.getAge()+"\t";
	}
	System.out.println(s);
	//要实现默认排序,必须实现Comparable接口,运用compareTo方法
	Collections.sort(list);
	String s1="默认排序:";
	for(Test test:list){
		s1+=test.getName()+"_"+test.getAge()+"\t";
	}
	System.out.println(s1);
	Comparator cmp=Collections.reverseOrder();
	Collections.sort(list, cmp);
	String s2="降序排序";
	for(Test test:list){
		s2+=test.getName()+"_"+test.getAge()+"\t";
	}
	System.out.println(s2);
	cmp=new Comparator(){
		@Override
		public int compare(Test o1, Test o2) {
			// TODO Auto-generated method stub
			return o1.getId()-o2.getId();
		}};
	Collections.sort(list, cmp);
	String s3="自定义:";
	for(Test test:list){
		s3+=test.getName()+"_"+test.getAge()+"\t";
	}
	System.out.println(s3);
}
@Override
public int compareTo(Object o) {
	// TODO Auto-generated method stub
	return this.age-((Test)o).getAge();
}
}
运行结果: java比较器以及集合类实现排序_第1张图片



你可能感兴趣的:(java)