📅  最后修改于: 2020-10-13 00:42:16             🧑  作者: Mango
Java Comparator接口用于对用户定义的类的对象进行排序。
该接口在java.util包中找到,包含2个方法compare(Object obj1,Object obj2)和equals(Object element)。
它提供了多种排序顺序,即,您可以根据任何数据成员(例如,rollno,名称,年龄或其他任何内容)对元素进行排序。
Method | Description |
---|---|
public int compare(Object obj1, Object obj2) | It compares the first object with the second object. |
public boolean equals(Object obj) | It is used to compare the current object with the specified object. |
public boolean equals(Object obj) | It is used to compare the current object with the specified object. |
Collections类提供用于对集合的元素进行排序的静态方法。如果集合元素是Set或Map,则可以使用TreeSet或TreeMap。但是,我们无法对List的元素进行排序。 Collections类还提供用于对List类型元素的元素进行排序的方法。
public void sort(List list,Comparator c):用于按给定的Comparator对List的元素进行排序。
让我们看一下根据年龄和姓名对List的元素进行排序的示例。在此示例中,我们创建了4个Java类:
此类包含三个字段rollno,name和age和一个参数化的构造函数。
class Student{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
此类根据年龄定义比较逻辑。如果第一个对象的年龄大于第二个对象的年龄,则我们将返回一个正值。可以是1、2、10之类的任何值。如果第一个对象的年龄小于第二个对象,我们将返回一个负值,它可以是任何负值,并且如果两个对象的年龄相等,我们返回0。
import java.util.*;
class AgeComparator implements Comparator{
public int compare(Object o1,Object o2){
Student s1=(Student)o1;
Student s2=(Student)o2;
if(s1.age==s2.age)
return 0;
else if(s1.age>s2.age)
return 1;
else
return -1;
}
}
此类提供基于名称的比较逻辑。在这种情况下,我们使用String类的compareTo()方法,该方法在内部提供比较逻辑。
import java.util.*;
class NameComparator implements Comparator{
public int compare(Object o1,Object o2){
Student s1=(Student)o1;
Student s2=(Student)o2;
return s1.name.compareTo(s2.name);
}
}
在此类中,我们将根据名称和年龄进行排序,以打印对象的值。
import java.util.*;
import java.io.*;
class Simple{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
System.out.println("Sorting by Name");
Collections.sort(al,new NameComparator());
Iterator itr=al.iterator();
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
System.out.println("Sorting by age");
Collections.sort(al,new AgeComparator());
Iterator itr2=al.iterator();
while(itr2.hasNext()){
Student st=(Student)itr2.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by age
105 Jai 21
101 Vijay 23
106 Ajay 27
class Student{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
import java.util.*;
class AgeComparator implements Comparator{
public int compare(Student s1,Student s2){
if(s1.age==s2.age)
return 0;
else if(s1.age>s2.age)
return 1;
else
return -1;
}
}
此类提供基于名称的比较逻辑。在这种情况下,我们使用String类的compareTo()方法,该方法在内部提供比较逻辑。
import java.util.*;
class NameComparator implements Comparator{
public int compare(Student s1,Student s2){
return s1.name.compareTo(s2.name);
}
}
在此类中,我们将根据名称和年龄进行排序,以打印对象的值。
import java.util.*;
import java.io.*;
class Simple{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
System.out.println("Sorting by Name");
Collections.sort(al,new NameComparator());
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
System.out.println("Sorting by age");
Collections.sort(al,new AgeComparator());
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by age
105 Jai 21
101 Vijay 23
106 Ajay 27
Java 8 Comparator接口是仅包含一个抽象方法的功能接口。现在,我们可以将Comparator接口用作lambda表达式或方法引用的分配目标。
Method | Description |
---|---|
int compare(T o1, T o2) | It compares the first object with second object. |
static Comparator |
It accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator |
static |
It accepts a function that extracts a sort key from a type T, and returns a Comparator |
static |
It accepts a function that extracts a double sort key from a type T, and returns a Comparator |
static |
It accepts a function that extracts an int sort key from a type T, and returns a Comparator |
static |
It accepts a function that extracts a long sort key from a type T, and returns a Comparator |
boolean equals(Object obj) | It is used to compare the current object with the specified object. |
static Comparator |
It returns a comparator that compares Comparable objects in natural order. |
static |
It returns a comparator that treats null to be less than non-null elements. |
static |
It returns a comparator that treats null to be greater than non-null elements. |
default Comparator |
It returns comparator that contains reverse ordering of the provided comparator. |
static |
It returns comparator that contains reverse of natural ordering. |
default Comparator |
It returns a lexicographic-order comparator with another comparator. |
default > Comparator |
It returns a lexicographic-order comparator with a function that extracts a Comparable sort key. |
default Comparator |
It returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator. |
default Comparator |
It returns a lexicographic-order comparator with a function that extracts a double sort key. |
default Comparator |
It returns a lexicographic-order comparator with a function that extracts a int sort key. |
default Comparator |
It returns a lexicographic-order comparator with a function that extracts a long sort key. |
让我们看一下根据年龄和姓名对List的元素进行排序的示例。
档案:Student.java
class Student {
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
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;
}
}
文件:TestSort1.java
import java.util.*;
public class TestSort1{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,"Jai",21));
//Sorting elements on the basis of name
Comparator cm1=Comparator.comparing(Student::getName);
Collections.sort(al,cm1);
System.out.println("Sorting by Name");
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
//Sorting elements on the basis of age
Comparator cm2=Comparator.comparing(Student::getAge);
Collections.sort(al,cm2);
System.out.println("Sorting by Age");
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by Age
105 Jai 21
101 Vijay 23
106 Ajay 27
在这里,我们对也包含null的元素列表进行排序。
档案:Student.java
class Student {
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
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;
}
}
文件:TestSort2.java
import java.util.*;
public class TestSort2{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(101,"Vijay",23));
al.add(new Student(106,"Ajay",27));
al.add(new Student(105,null,21));
Comparator cm1=Comparator.comparing(Student::getName,Comparator.nullsFirst(String::compareTo));
Collections.sort(al,cm1);
System.out.println("Considers null to be less than non-null");
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
Comparator cm2=Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo));
Collections.sort(al,cm2);
System.out.println("Considers null to be greater than non-null");
for(Student st: al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Considers null to be less than non-null
105 null 21
106 Ajay 27
101 Vijay 23
Considers null to be greater than non-null
106 Ajay 27
101 Vijay 23
105 null 21