@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.*@returntrue
onlyifthespecifiedobjectisalso*acomparatoranditimposesthesameorderingasthis*comparator.*@seeObject#equals(Object)*@seeObject#hashCode()*/booleanequals(Objectobj);接口中明明有两个抽象方法,intcompare(To1,To2);和booleanequals(Objectobj);为何还是函数式接口???
繁花如伊
交互式爱情
相关分类