为了账号安全,请及时绑定邮箱和手机立即绑定

java Comparator为何是函数式接口?

java Comparator为何是函数式接口?

素胚勾勒不出你 2019-05-14 15:16:39
@FunctionalInterfacepublicinterfaceComparator{/***Comparesitstwoargumentsfororder.Returnsanegativeinteger,*zero,orapositiveintegerasthefirstargumentislessthan,equal*to,orgreaterthanthesecond.**Intheforegoingdescription,thenotation*sgn(expression)designatesthemathematical*signumfunction,whichisdefinedtoreturnoneof-1,*0,or1accordingtowhetherthevalueof*expressionisnegative,zeroorpositive.**Theimplementormustensurethatsgn(compare(x,y))==*-sgn(compare(y,x))forallxandy.(This*impliesthatcompare(x,y)mustthrowanexceptionifandonly*ifcompare(y,x)throwsanexception.)**Theimplementormustalsoensurethattherelationistransitive:*((compare(x,y)>0)&&(compare(y,z)>0))implies*compare(x,z)>0.**Finally,theimplementormustensurethatcompare(x,y)==0*impliesthatsgn(compare(x,z))==sgn(compare(y,z))forall*z.**Itisgenerallythecase,butnotstrictlyrequiredthat*(compare(x,y)==0)==(x.equals(y)).Generallyspeaking,*anycomparatorthatviolatesthisconditionshouldclearlyindicate*thisfact.Therecommendedlanguageis"Note:thiscomparator*imposesorderingsthatareinconsistentwithequals."**@paramo1thefirstobjecttobecompared.*@paramo2thesecondobjecttobecompared.*@returnanegativeinteger,zero,orapositiveintegerasthe*firstargumentislessthan,equalto,orgreaterthanthe*second.*@throwsNullPointerExceptionifanargumentisnullandthis*comparatordoesnotpermitnullarguments*@throwsClassCastExceptionifthearguments'typespreventthemfrom*beingcomparedbythiscomparator.*/intcompare(To1,To2);/***Indicateswhethersomeotherobjectis"equalto"this*comparator.Thismethodmustobeythegeneralcontractof*{@linkObject#equals(Object)}.Additionally,thismethodcanreturn*trueonlyifthespecifiedobjectisalsoacomparator*anditimposesthesameorderingasthiscomparator.Thus,*comp1.equals(comp2)impliesthatsgn(comp1.compare(o1,*o2))==sgn(comp2.compare(o1,o2))foreveryobjectreference*o1ando2.**Notethatitisalwayssafenottooverride*Object.equals(Object).However,overridingthismethodmay,*insomecases,improveperformancebyallowingprogramstodetermine*thattwodistinctcomparatorsimposethesameorder.**@paramobjthereferenceobjectwithwhichtocompare.*@returntrueonlyifthespecifiedobjectisalso*acomparatoranditimposesthesameorderingasthis*comparator.*@seeObject#equals(Object)*@seeObject#hashCode()*/booleanequals(Objectobj);接口中明明有两个抽象方法,intcompare(To1,To2);和booleanequals(Objectobj);为何还是函数式接口???
查看完整描述

2 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

Thismethodmustobeythegeneralcontractof{@linkObject#equals(Object)}.
                            
查看完整回答
反对 回复 2019-05-14
?
交互式爱情

TA贡献1712条经验 获得超3个赞

public@interfaceFunctionalInterface官方文档:
Ifaninterfacedeclaresanabstractmethodoverridingoneofthepublicmethodsofjava.lang.Object,thatalsodoesnotcounttowardtheinterface'sabstractmethodcountsinceanyimplementationoftheinterfacewillhaveanimplementationfromjava.lang.Objectorelsewhere.
如果接口声明了一个覆盖java.lang.Object的全局方法之一的抽象方法,那么它不会计入接口的抽象方法数量中,因为接口的任何实现都将具有java.lang.Object或其他地方的实现。
                            
查看完整回答
反对 回复 2019-05-14
  • 2 回答
  • 0 关注
  • 1233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信