Comparable接口和compareTo()函数

article/2025/10/31 6:08:45

Comparable

Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较,则依赖compareTo方法的实现,compareTo方法也被称为自然比较方法。如果开发者add进入一个Collection的对象想要Collections的sort方法帮你自动进行排序的话,那么这个对象必须实现Comparable接口。compareTo方法的返回值是int,有三种情况:

1、比较者大于被比较者(也就是compareTo方法里面的对象),那么返回正整数

2、比较者等于被比较者,那么返回0

3、比较者小于被比较者,那么返回负整数

为什么需要实现Comparable接口

写个很简单的例子:

public class test {public static void main(String[] args){ArrayList list=new ArrayList();list.add("Java");list.add("rose");list.add("lucy");System.out.println(list);Collections.sort(list);System.out.println(list);}
}

我们来排序输出一下结果

[Java, rose, lucy]
[Java, lucy, rose]

好像并没有什么问题。但是当List容器添加的元素对象是属于自己写的类时, 就可能出问题了.

例子:


import java.util.ArrayList;
import java.util.Collections;class Student{private String name;private int age;public Student(String name, int age){this.name = name;this.age = age;} public String toString(){return this.name + ":" + this.age;}
}public class CompareT{public static void add(){ArrayList list = new ArrayList();list.add(new Student("Jack",10));list.add(new Student("rose",11));list.add(new Student("lucy",27));System.out.println(list);} 

上面定义了1个Student类, 它只有两个成员, 名字和年龄.

在add()方法内, 添加3个Student的对象到1个list容器中, 然后输出(必须重写String方法, 这里不解释了):

执行结果:

在这里插入图片描述

报错了 ClassCastException异常

提示这个类Student没有实现Comparable接口.

原因也很简单, 因为Java不知道应该怎样为Student对象排序, 是应该按名字排序? 还是按age来排序?

为什么本文第1个例子就排序成功? 是因为Java本身提供的类Integer已经实现了Comparable接口. 也表明Integer这个类的对象是可以比较的.

而Student类的对象默认是不可以比较的. 除非它实现了Comparable接口.

总而言之, 如果你想1个类的对象支持比较(排序), 就需要实现Comparable接口.

Comparable接口简介

Comparable 接口内部只有1个要重写的关键的方法.

就是

int compareTo(T o)

这个方法返回1个Int数值,

例如 i = x.compareTo(y)

如果i=0, 也表明对象x与y排位上是相等的(并非意味x.equals(y) = true, 但是jdk api上强烈建议这样处理)

如果返回数值i>0 则意味者, x > y啦,

反之若i<0则 意味x < y

什么是compareTo函数

compareTo() 方法用于将对象与方法的参数进行比较。

public interface Comparable<T> {/*** Compares this object with the specified object for order.  Returns a* negative integer, zero, or a positive integer as this object is less* than, equal to, or greater than the specified object.** <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This* implies that <tt>x.compareTo(y)</tt> must throw an exception iff* <tt>y.compareTo(x)</tt> throws an exception.)** <p>The implementor must also ensure that the relation is transitive:* <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies* <tt>x.compareTo(z)&gt;0</tt>.** <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for* all <tt>z</tt>.** <p>It is strongly recommended, but <i>not</i> strictly required that* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any* class that implements the <tt>Comparable</tt> interface and violates* this condition should clearly indicate this fact.  The recommended* language is "Note: this class has a natural ordering that is* inconsistent with equals."** <p>In the foregoing description, the notation* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,* <tt>0</tt>, or <tt>1</tt> according to whether the value of* <i>expression</i> is negative, zero or positive.** @param   o the object to be compared.* @return  a negative integer, zero, or a positive integer as this object*          is less than, equal to, or greater than the specified object.** @throws NullPointerException if the specified object is null* @throws ClassCastException if the specified object's type prevents it*         from being compared to this object.*/public int compareTo(T o);

官方给出的解释是

  • 将此对象与指定的order对象进行比较。返回一个负整数,零,或正整数,因为这个对象比较小于、等于或大于指定的对象。

  • 实现者必须确保所有和x和y的比较如果出现异常必须确保抛出一个异常

  • 最后,实现者必须确保 x.compareTo(y)==0,(x.compareTo(y)>0 && y.compareTo(z)>0)相当于x.compareTo(z)>0

  • 强烈建议(x.compareTo(y)==0) == (x.equals(y)),但不是严格要求必须这么写,任何实现comparable接口并违反此条件的类都应该清楚地指出这一事实。该类的自然顺序与equals不一致

参数

  • @param o要比较的对象。
  • @return 一个负整数、零或正整数作为该对象小于、等于或大于指定的对象。
  • @throws NullPointerException 指定对象为空抛出NullPointerException 异常
  • @throws ClassCastException 如果指定对象的类型组织将其与此对象进行比较,抛出ClassCastException异常

位置

package java.lang;import java.util.*;

位于lang包下

是Compareable下的一个方法,参数是一个泛型

实现

compareTo具体实现在Number类和String类中

Number类中的实现–https://www.tutorialspoint.com/java/number_compareto.htm

在这里插入图片描述

String类中的实现–https://www.tutorialspoint.com/java/java_string_compareto.htm

在这里插入图片描述

Number类中的实现

1.描述

该方法将调用方法的Number对象与参数进行比较。可以比较Byte,Long,Integer等。

但是,无法比较两种不同的类型,参数和调用方法的Number对象应该是相同的类型。

2.语法

public int compareTo( NumberSubClass referenceName )

3.参数

这是参数的细节 -

  • referenceName-可以是Byte,Double,Integer,Float,Long或Short。

4.返回值

  • 如果Integer等于参数,则返回0。
  • 如果Integer小于参数,则返回-1。
  • 如果Integer大于参数,则返回1。

5.例子

public class Test { public static void main(String args[]) {Integer x = 5;System.out.println(x.compareTo(3));System.out.println(x.compareTo(5));System.out.println(x.compareTo(8));            }
}

6.结果

1
0
-1

String类中的实现

官方的文档可以看到compareTo()在String类中有两种实现

1.第一种传入的参数可以是一个Object的对象

注意:compareTo()的接口传入的是一个泛型而不是一个Object对象这里注意一下

2.第二种传入的参数可以是一个任意的字符串

在这里插入图片描述

传入对象的情况

1.描述

此方法将String与另一个Object进行比较。

2.语法

一下是此方法的语法

int compareTo(Object o)

3.参数

  • O —要比较的对象

4.返回值

如果参数是一个按字典顺序排列等于该字符串的字符串,则值为0; 如果参数是按字典顺序大于此字符串的字符串,则小于0的值; 如果参数是按字典顺序小于此字符串的字符串,则值大于0。

5.例子

public class Test {public static void main(String args[]) {String str1 = "Strings are immutable";String str2 = new String("Strings are immutable");String str3 = new String("Integers are not immutable");int result = str1.compareTo( str2 );System.out.println(result);result = str2.compareTo( str3 );System.out.println(result);}
}

6.结果

0
10

传入字符串的情况

1.描述

此方法按字典顺序比较两个字符串。

2.语法

int compareTo(String anotherString)

3.参数

  • anotherString - 要比较的String。

4.返回值

如果参数是一个按字典顺序排列等于该字符串的字符串,则值为0; 如果参数是按字典顺序大于此字符串的字符串,则小于0的值; 如果参数是按字典顺序小于此字符串的字符串,则值大于0。

5.例子

public class Test {public static void main(String args[]) {String str1 = "Strings are immutable";String str2 = "Strings are immutable";String str3 = "Integers are not immutable";int result = str1.compareTo( str2 );System.out.println(result);result = str2.compareTo( str3 );System.out.println(result);result = str3.compareTo( str1 );System.out.println(result);}
}

6.结果

0
10
-10

下一篇继续介绍Comparator 外部比较器

​ --作者:额滴神


http://chatgpt.dhexx.cn/article/2eBfCft3.shtml

相关文章

Compareable接口的compareTo方法详解

Compareable接口可以实现类中成员属性的排序方法。通过重写Compareable接口中的CompareTo方法实现自定义规则的排序。 针对Compareable接口的排序方式,将通过对学生类和测试类进行一个代码演示 一般情况下 一般情况下Compareable接口是实现自然排序,即依照对象的添加顺序进行…

关于比较CompareTo的用法

CompareTo的比较原理&#xff1a; 先读取出字符串的第一个“字母”进行比较&#xff0c;比较的方法是ascii码表的值&#xff08;字符所对应的十进制值&#xff09;&#xff0c;如果前面的大那么返回1&#xff0c;后面的大返回-1&#xff1b;此位置相同&#xff0c;继续比较下一…

Java compare方法和compareTo方法

1.compare 对于 public int compare(Object arg0, Object arg1)的理解 //数组排序 String[] str new String[5]; Arrays.sort(str, new Comparator<String>() {Overridepublic int compare(String o1, String o2) {// TODO Auto-generated method stubreturn 0;} }); …

compareTo比较大小

public class TestInteger {public static void main(String[] args){//比较大小Integer i1new Integer(3);Integer i2new Integer(5);System.out.println(i1.compareTo(i2));} }因为i1<i2&#xff0c;所以输出的是-1&#xff1b; compareTo() 方法用于将 Number 对象与方法…

Java之比较CompareTo的用法

CompareTo的比较原理&#xff1a; 先读取出字符串的第一个“字母”进行比较&#xff0c;比较的方法是ascii码表的值&#xff08;字符所对应的十进制值&#xff09;&#xff0c;如果前面的大那么返回1&#xff0c;后面的大返回-1&#xff1b;此位置相同&#xff0c;继续比较下一…

MFC修改AfxMessageBox对话框标题

AfxMessageBox的对话框标题默认为项目工程的名字&#xff0c;对话框一般是为了给用户提示相关信息&#xff0c;而软件名字一般都与项目工程名不一样&#xff0c;例如软件可能是中文名。 1. 在资源视图&#xff0c;在String Table没有AFX_IDS_APP_TITLE 2. 添加字符串“AFX_IDS…

VS mfc MessageBox() 和 AfxMessageBox()

转载&#xff1a;http://blog.csdn.net/phenixyf/article/details/41744039 一、MessageBox()用法 1、函数原型 Messagebox函数在Win32 API和MFC里的定义有区别。 Win32 API的定义如下&#xff1a; [cpp] view plain copy int WINAPI MessageBox( HWND hWnd, // h…

修改MFC中AfxMessageBox()函数的对话框标题

修改MFC中AfxMessageBox()函数的对话框标题 如何在MFC中修改AfxMessageBox()函数所弹出的对话框标题&#xff0c;步骤如下&#xff1a; 1.找到项目工程的资源视图&#xff0c;打开.rc资源文件下的String Table资源&#xff1b; 2.在String Table资源找到ID号为AFX_IDS_APP_T…

AfxMessageBox的常见应用

新建一个基于对话框的MFC应用程序AfxMessageBoxTest&#xff0c;删除对话框中原有控件。添加3个按钮&#xff0c;为每个按钮添加一些文字&#xff0c;如下图&#xff1a; 从上到下&#xff0c;依次为每个按钮添加事件函数&#xff0c;第一个按钮代码如下&#xff1a; void CA…

MFC 修改AfxMessageBox默认标题

双击新建的字符串ID&#xff0c;改为**AFX_IDS_APP_TITLE**&#xff1a; 修改标题为“温馨提示”

AfxMessageBox与MessageBox函数

创建消息提示对话框&#xff1a; 前者调用的是MFC的底层函数库&#xff0c;后者调用的是最为基本的Win32 API。 MessageBox()的函数原型如下&#xff1a; int MessageBox(LPCTSTR lpszText,LPCTSTR lpszCaption NULL,UINT nType MB_OK );参数说明&#xff1a; lpszText&am…

AfxMessageBox和MessageBox的用法

目录 一、AfxMessageBox 1. 函数定义原型 3. 返回值 二、MessageBox 1. 定义 ​2. 参数介绍 三、 AfxMessageBox和MessageBox的区别 1. 区别一 2. 区别二 3. 区别三 一、AfxMessageBox AfxMessageBox是一种函数原型&#xff0c;特点是应用程序的可执行文件&#xff…

AfxMessageBox 自定义封装

一般情况下AfxMessageBox是系统提供的一个对话框&#xff0c;若要做这种效果的&#xff0c;必须重写。 实例1&#xff1a; void test_SgxMemDialog_AutoSize() { //使用给定大小的对话框 CSgxMemDialog dlg(180, 60); dlg.SetWindowTitle(_T(" SegeX - CT&qu…

AfxMessageBox()函数消息框按钮和图标

消息框中显示的按钮 CString str1;str1.Format(_T("MB_ABORTRETRYIGNORE"), height, width);//消息框包含三个按钮&#xff1a;Abort&#xff0c;Retry和IgnoreAfxMessageBox(str1, MB_ABORTRETRYIGNORE | MB_ICONINFORMATION, 0);CString str2;str2.Format(_T(&quo…

VC中MessageBox与AfxMessageBox用法与区别

一、MessageBox()用法 1、函数原型 Messagebox函数在Win32 API和MFC里的定义有区别。 Win32 API的定义如下&#xff1a; int WINAPI MessageBox(HWND hWnd, // handle of owner windowLPCTSTR lpText, // address of text in message boxLPCTSTR lpCaption, //…

MFC:AfxMessageBox函数随记

函数原型 函数原型1 int AfxMessageBox( LPCTSTR lpszText, UINT nType MB_OK, UINT nIDHelp 0 );lpszText:弹窗内容 nType &#xff1a;弹窗类型和按钮类型进行或&#xff0c;比如询问框等 函数原型2 int AFXAPI AfxMessageBox( UINT nIDPrompt, UINT nType MB_OK, UIN…

MFC之MessageBox、AfxMessageBox用法

在软件中我们经常会弹出个小窗口,给一点点提示.这就会用到消息对话框.在Win32 API程序中只有MessageBox这一种用法. 而在MFC中就有三各方法: 1.调用API中的MessageBox&#xff1b; 2.调用CWnd的成员函数MessageBox&#xff1b; 3.调用全局函数AfxMessageBox&#xff1b; M…

字符串数组的引用(C语言实现)

一、字符串数组的定义 1.字符串的定义&#xff1a;是由若干有效字符构成且以字符"\0"作为结束标志的一个字符序列. 2.字符串的实现&#xff1a; 因为C语言中只提供字符数据类型&#xff0c;没有提供字符串数据类型&#xff0c;所有C语言中的 字符串是通过字符数组来…

Java输出字符串数组

直接toString会输出一串地址&#xff0c; 建议使用Arrays.toString( ),将数组转化为一个字符串输出; 如果要一个一个输出还是遍历 public static void main(String[] args) {String exp"12,23,34";String res[]exp.split(",");System.out.println(Arrays.…

c+字符串数组_了解C ++字符串数组

c+字符串数组 Hey, Folks! So, as programmers, we often deal with Arrays of all data types. We’ll cover C++ string array in today’s article. 嘿伙计! 因此,作为程序员,我们经常处理所有数据类型的数组。 我们将在今天的文章中介绍C ++字符串数组 。 声明C ++字符…