Comparable与Comparator，java中的排序与比较

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## Comparable与Comparator，java中的排序与比较

luminji 2017-08-09 11:33:00 浏览506

1：比较和排序的概念

比较：两个实体类之间按>，=，<进行比较。

排序：在集合类中，对集合类中的实体进行排序。排序基于的算法基于实体类提供的比较函数。

基本型别都提供了默认的比较算法，如string提供了按字母进行比较，int提供了按整数大小进行比较。

2：Comparable与Comparator

but，光看概念没有任何意义，我们不知道这对代码来说意味着什么；

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

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

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
Collections.sort(students);
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

class Student implements Comparable{
public String name;
public int age;
public Student(String name, int age){
this.name = name;
this.age = age;
}

@Override
public int compareTo(Object o) {
Student student = (Student)o;
if (age> student.age)
{
return 1;
}
else if (age == student.age)
{
return 0;
}
else
{
return -1;
}
//return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
}
}

name:b, age: 1
name:c, age: 2
name:a, age: 3
name:d, age: 4

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
//Collections.sort(students);
students.sort(new NameSorter());
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

class Student implements Comparable{
public String name;
public int age;
public Student(String name, int age){
this.name = name;
this.age = age;
}

@Override
public int compareTo(Object o) {
Student student = (Student)o;
if (age> student.age)
{
return 1;
}
else if (age == student.age)
{
return 0;
}
else
{
return -1;
}
//return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
}
}

class NameSorter implements Comparator{

@Override
public int compare(Object o1, Object o2) {
Student s1 =(Student) o1;
Student s2 =(Student) o2;
return s1.name.compareTo(s2.name);
}
}

name:a, age: 3
name:b, age: 1
name:c, age: 2
name:d, age: 4

3：Comparable与Comparator的泛型版本Comparable<T>与Comparator<T>

Student s1 =(Student) o1;
Student s2 =(Student) o2;

package com.zuikc.se.collectors;

/**
* 排序和比较器的用法，Comparable与Comparator，java中的排序与比较
* author:最课程（zuikc.com）
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
//Collections.sort(students);
students.sort(new NameSorter());
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age){
this.name = name;
this.age = age;
}

@Override
public int compareTo(Student student) {
if (age> student.age)
{
return 1;
}
else if (age == student.age)
{
return 0;
}
else
{
return -1;
}
//return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
}
}

class NameSorter implements Comparator<Student>{

@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name);
}
}

luminji
+ 关注