第六节:Java中元素和对象的比较方法
Posted 快乐江湖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第六节:Java中元素和对象的比较方法相关的知识,希望对你有一定的参考价值。
文章目录
一:元素比较
(1)基本类型比较
基本类型比较:在Java中,基本类型是可以直接进行比较的
public class TestDemo
public static void main(String[] args)
// 数值类型比较
int a = 10;
int b = 20;
System.out.println(a > b);
System.out.println(a < b);
// 字符类型比较
char c = 'A';
char d = 'B';
System.out.println(c > d);
System.out.println(c < d);
// 布尔类型比较
boolean e = true;
boolean f = false;
System.out.println(e == f);
System.out.println(e != f);
(2)引用类型比较
引用类型比较:在Java中,引用类型是不能采用<
或>
进行比较的,因为引用类型在比较时比较的是引用变量的地址
student1 == student3
结果为false
是因为它们是不同的对象,虽然内容相同student1 == student3
结果为true
是因为它们是相同的对象
class Student
int age;
String name;
public Student(int age, String name)
this.age = age;
this.name = name;
public class TestDemo
public static void main(String[] args)
Student student1 = new Student(17, "张三");
Student student2 = new Student(19, "李四");
Student student3 = new Student(17, "张三");
Student student4 = student1;
System.out.println(student1 < student2);//无法编译
System.out.println(student1 == student3);
System.out.println(student1 == student4);
其实,对于用户自定义类型在进行比较时会默认调用equal
方法(继承自Object
),而上面的==
之所以可以比较是因为它默认调用的也是equal
方法
public boolean equals(Object obj)
return (this==obj);
二:对象比较
很多情况下,我们需要根据对象中的内容来对对象进行比较,主要有如下三种方法
-
重写父类的
equals
方法:因为所有类都是继承自Object
的,所以直接重写即可,不过只能比较相等与否 -
基于
Comparable
接口类的比较:需要手动实现接口,侵入性比较强,但一旦实现,每次用该类都有顺序,属于内部顺序 -
基于
Comparator
比较器的比较:需要实现一个比较器对象,对待比较类的侵入性弱,但对算法代码实现侵入性而强
(1)重写父类的equals方法
重写父类的equals
方法虽然可行,但是它只能按照相等的方式进行比较,而不能比较大于或小于
class Student
int age;
String name;
public Student(int age, String name)
this.age = age;
this.name = name;
@Override
public boolean equals(Object o)
if(this == o)
return true;
//o如果是null对象或者o不是Student的子类
if(o == null || !(o instanceof Student))
return false;
Student student = (Student)o;
return this.age == student.age && this.name.equals(student.name);
public class TestDemo
public static void main(String[] args)
Student student1 = new Student(17, "张三");
Student student2 = new Student(19, "李四");
Student student3 = new Student(17, "张三");
System.out.println(student1.equals(student2));
System.out.println(student1.equals(student3));
(2)基于Comparable接口类的比较
Comparable:它是Java提供的泛型的比较类接口,具体实现如下
<0
:表示当前对象this
小于对象o
=0
:表示当前对象this
等于对象o
>0
:表示当前对象this
大于对象o
public interface Comparable<E>
int compareTo(E 0)
所以对于自定义类型,可以实现Comparable
接口,然后在类中重写compareTo
方法,制定自己的比较规则
class Student implements Comparable<Student>
int age;
String name;
public Student(int age, String name)
this.age = age;
this.name = name;
@Override
public int compareTo(Student o)
if(o == null)
return 1;
if(this.age == o.age && this.name.equals(o.name))
return 0;
return this.age - student.age;
public class TestDemo
public static void main(String[] args)
Student student1 = new Student(17, "张三");
Student student2 = new Student(19, "李四");
Student student3 = new Student(17, "张三");
System.out.println(student1.compareTo(student3));
System.out.println(student1.compareTo(student2));
System.out.println(student2.compareTo(student3));
(3)基于Comparator比较器的比较
Comparator:它是Java提供的泛型接口类,使用步骤如下
- 用户自定义比较器类,实现
Comparator
接口 - 重写
Comparator
中的compare
方法
import java.util.Comparator;
class Student
int age;
String name;
public Student(int age, String name)
this.age = age;
this.name = name;
class StudentComparator implements Comparator<Student>
@Override
public int compare(Student o1, Student o2)
if(o1 == o2)
return 0;
if(o1 == null)
return -1;
if(o2 == null)
return 1;
if(o1.age == o2.age && o1.name.equals(o2.name))
return 0;
return o1.age - o2.age;
public class TestDemo
public static void main(String[] args)
Student student1 = new Student(17, "张三");
Student student2 = new Student(19, "李四");
Student student3 = new Student(17, "张三");
StudentComparator studentComparator = new StudentComparator();
System.out.println(studentComparator.compare(student1, student3));
System.out.println(studentComparator.compare(student1, student2));
System.out.println(studentComparator.compare(student2, student3));
以上是关于第六节:Java中元素和对象的比较方法的主要内容,如果未能解决你的问题,请参考以下文章