版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JavaComparator源码总结Comparator源码注释翻译和解析中英⽂对照版版本JDK8(JDK1.8)Comparator接⼝重点1.Comparator接⼝是⼀个函数式接⼝,⾥⾯只有⼀个虚⽅法compare(To1,To2),该接⼝表⽰⼀个⽐较器,⽽实现Comparable接⼝则表⽰其实现类是可⽐较的,Comparable接⼝的虚⽅法是compareTo(To)Comparable接⼝源码可以看我这篇位置2.compare(To1,To2)返回值⼤于0,表⽰o1-o2>0,若o1,o2相等,则compare(To1,To2)返回03.Comparator接⼝⽅法注:then开头⽐较器都是⼆次⽐较器,在第⼀次调⽤pare(To1,To2)两者相等,才会使⽤⼀个新的⽐较器来区分两者顺序,如果第⼀次都不相等,则不会调⽤后续⽐较器⽅法名作⽤intcompare(To1,To2)返回o1-o2的值Comparatorcomparing(FunctionkeyExtractor)返回⼀个新的⽐较器,其⽐较规则先经过keyExtractor处理o1和o2后,再将处理后的值相减c⽐较⼤⼩(类似相减,其实是调⽤compareTo)Comparatorcomparing(FunctionkeyExtractor,ComparatorkeyComparator)返回⼀个新的⽐较器,其⽐较规则先经过keyExtractor处理o1和o2后,再将使⽤keyComparator⽐较器的规则⽐较⼤⼩ComparatorcomparingInt(ToIntFunctionkeyExtractor)和上⾯comparing(FunctionkeyExtractor)差不多,只不过ToIntFunction处理后得到的值是Int,则再两个Int相减ComparatorcomparingLong(ToLongFunctionkeyExtractor)和上⾯comparing(FunctionkeyExtractor)差不多,只不过ToLongFunction处理后得到的值是Long,则再两个Long相减ComparatorcomparingDouble(ToDoubleFunctionkeyExtractor)和上⾯comparing(FunctionkeyExtractor)差不多,只不过ToDoubleFunction处理后得到的值是Double,则再两个Double相减ComparatornaturalOrder()ComparatorreverseOrder()返回⼀个⾃然顺序⽐较器,⽐较规则为pareTo(o2),相当于o1-o2返回⼀个与⾃然顺序⽐较器相反的⽐较器,⽐较规则为pareTo(o1),相当于o2-o1ComparatonullsFirst(Comparatorcomparator)返回⼀个空友好的⽐较器,该⽐较器认为null⼩于⾮空,两个null相等ComparatornullsLast(Comparatorcomparator)返回⼀个空友好的⽐较器,该⽐较器认为null⼤于⾮空,两个null相等Comparatorreversed()返回⼀个新的⽐较器,它的compare(To1,To2)的作⽤变成返回o2-o1的值,即相反ComparatorthenComparing(Comparator返回⼀个新的⽐较器,当调⽤pare(To1,To2)时o1和o2相等,再调⽤pare(To1,other)To2)⽐较o1,o2,如果第⼀步不相等则直接返回,下⾯也⼀样ComparatorthenComparing(FunctionkeyExtractor)当调⽤pare(To1,To2)时o1和o2相等,上⾯的other⽐较器⽤以下步骤⽣成的⽐较器替代,该⽐较器符合以下步骤,会先⽤keyExtractor处理o1,o2,然后直接把处理后的值相减ComparatorthenComparing(FunctionkeyExtractor,ComparatorkeyComparator)当调⽤pare(To1,To2)时o1和o2相等,上⾯的other⽐较器⽤以下步骤⽣成的⽐较器替代,该⽐较器符合以下步骤,先⽤keyExtractor处理o1,o2,再调⽤keyComparator⽐较处理后的值ComparatorthenComparingInt(ToIntFunctionkeyExtractor)和上⾯thenComparing(FunctionkeyExtractor)差不多,只是只不过ToIntFunction处理后得到的值是Int,然后两个Int相减ComparatorthenComparingLong(ToLongFunctionkeyExtractor)和上⾯thenComparing(FunctionkeyExtractor)差不多,只不过ToLongFunction处理后得到的值是Long,则再两个Long相减keyExtractor)⽅法名作⽤Comparator和上⾯thenComparing(FunctionkeyExtractor)差不多,只不过ToDoubleFunction处理后得到的值是Double,则再两个Double相减thenComparingDouble(ToDoubleFunctionkeyExtractor)Comparator接⼝源码packagejava.util;importjava.io.Serializable;importjava.util.function.Function;importjava.util.function.ToIntFunction;importjava.util.function.ToLongFunction;importjava.util.function.ToDoubleFunction;importjava.util.Comparators;/***Acomparisonfunction,whichimposesa<i>totalordering</i>onsome*collectionofobjects.Comparatorscanbepassedtoasortmethod(such*as{@linkCollections#sort(List,Comparator)Collections.sort}or{@link*Arrays#sort(Object[],Comparator)Arrays.sort})toallowprecisecontrol*overthesortorder.Comparatorscanalsobeusedtocontroltheorderof*certaindatastructures(suchas{@linkSortedSetsortedsets}or{@link*SortedMapsortedmaps}),ortoprovideanorderingforcollectionsof*objectsthatdon'thavea{@linkComparablenaturalordering}.<p>*⼀种⽐较函数,它对某些对象集合施加总排序。*可以将⽐较器传递给排序⽅法(例如Collections#sort(List,Comparator)或*Arrays#sort(Object[],Comparator)),以便精确控制排序顺序。*⽐较器还可⽤于控制某些数据结构的顺序(如SortedSet或SortedMap),*或为没有Comparable⾃然顺序的对象集合提供排序。**Theorderingimposedbyacomparator{@codec}onasetofelements*{@codeS}issaidtobe<i>consistentwithequals</i>ifandonlyif*{@codepare(e1,e2)==0}hasthesamebooleanvalueas*{@codee1.equals(e2)}forevery{@codee1}and{@codee2}in*{@codeS}.<p>*当且仅当pare(e1,e2)具有与e1和e2中的每⼀个e1和e2相同的布尔值时,*⽐较器c对⼀组元素S施加的排序称为与等于⼀致。*****Cautionshouldbeexercisedwhenusingacomparatorcapableofimposingan*orderinginconsistentwithequalstoorderasortedset(orsortedmap).*Supposeasortedset(orsortedmap)withanexplicitcomparator{@codec}*isusedwithelements(orkeys)drawnfromaset{@codeS}.Ifthe*orderingimposedby{@codec}on{@codeS}isinconsistentwithequals,*thesortedset(orsortedmap)willbehave"strangely."Inparticularthe*sortedset(orsortedmap)willviolatethegeneralcontractforset(or*map),whichisdefinedintermsof{@codeequals}.<p>*当使⽤⽐较器时,应谨慎使⽤,*⽐较器能够对排序集(或排序映射)施加与等于不⼀致的排序。*假设⼀个带有显式⽐较器c的排序集(或排序映射)与从集合S中提取的元素(或键)⼀起使⽤。*如果c对S施加的排序与等于不⼀致,则排序集(或排序映射)的⾏为将“奇怪”。*特别是排序集(或排序映射)将违反集合(或映射)的⼀般契约,*该契约是根据equals定义的。******Forexample,supposeoneaddstwoelements{@codea}and{@codeb}suchthat*{@code(a.equals(b)&&pare(a,b)!=0)}*toanempty{@codeTreeSet}withcomparator{@codec}.*Thesecond{@codeadd}operationwillreturn*Thesecond{@codeadd}operationwillreturn*true(andthesizeofthetreesetwillincrease)because{@codea}and*{@codeb}arenotequivalentfromthetreeset'sperspective,eventhough*thisiscontrarytothespecificationofthe*{@linkSet#addSet.add}method.<p>*例如,假设添加两个元素a和b,使得(a.equals(b)&&pare(a,b)!=0)*到带有⽐较器c的空TreeSet。第⼆个add操作将返回true(树集的⼤⼩将增加),*因为从树集的⾓度来看a和b并不等价,即使这与set#add⽅法的规范相反。******Note:Itisgenerallyagoodideaforcomparatorstoalsoimplement*{@codejava.io.Serializable},astheymaybeusedasorderingmethodsin*serializabledatastructures(like{@linkTreeSet},{@linkTreeMap}).In*orderforthedatastructuretoserializesuccessfully,thecomparator(if*provided)mustimplement{@codeSerializable}.<p>*注意:⽐较器也实现java.io.Serializable通常是⼀个好主意,*因为它们可以⽤作可序列化数据结构(如TreeSet、TreeMap)中的排序⽅法。*为了成功序列化数据结构,⽐较器(如果提供)必须实现Serializable*****Forthemathematicallyinclined,the<i>relation</i>thatdefinesthe*<i>imposedordering</i>thatagivencomparator{@codec}imposesona*givensetofobjects{@codeS}is:*对于数学上倾向的,定义给定⽐较器c对给定对象集S施加的强制顺序的关系为:**<pre>*{(x,y)suchthatpare(x,y)<=0}.*</pre>*The<i>quotient</i>forthistotalorderis:*此总顺序的商为:*<pre>*{(x,y)suchthatpare(x,y)==0}.*</pre>**Itfollowsimmediatelyfromthecontractfor{@codecompare}thatthe*quotientisan<i>equivalencerelation</i>on{@codeS},andthatthe*imposedorderingisa<i>totalorder</i>on{@codeS}.Whenwesaythat*theorderingimposedby{@codec}on{@codeS}is<i>consistentwith*equals</i>,wemeanthatthequotientfortheorderingistheequivalence*relationdefinedbytheobjects'{@linkObject#equals(Object)*equals(Object)}method(s):*紧接着从compare的契约可以看出,商是S上的等价关系,*并且强加的顺序是S上的总顺序。当我们说c对S施加的排序与等于⼀致时,*我们的意思是排序的商是由对象Object#equals(Object)⽅法定义的等价关系****<pre>*{(x,y)suchthatx.equals(y)}.*</pre>**<p>Unlike{@codeComparable},acomparatormayoptionallypermit*comparisonofnullarguments,whilemaintainingtherequirementsfor*anequivalencerelation.*与Comparable不同,⽐较器可以选择允许⽐较空参数,同时保持对等价关系的要求。**<p>Thisinterfaceisamemberofthe*<ahref="{@docRoot}/java/util/package-summary.html#CollectionsFramework">*JavaCollectionsFramework</a>.**@param<T>thetypeofobjectsthatmaybecomparedbythiscomparator*@param<T>thetypeofobjectsthatmaybecomparedbythiscomparator**@authorJoshBloch*@authorNealGafter*@seeComparable*@seejava.io.Serializable*@since1.2*/@FunctionalInterfacepublicinterfaceComparator<T>{/***Comparesitstwoargumentsfororder.Returnsanegativeinteger,*zero,orapositiveintegerasthefirstargumentislessthan,equal*to,orgreaterthanthesecond.<p>*⽐较其两个参数的顺序。返回负整数、零或正整数,*因为第⼀个参数⼩于、等于或⼤于第⼆个参数。**Theimplementormustensurethat{@codesgn(compare(x,y))==*-sgn(compare(y,x))}forall{@codex}and{@codey}.(This*impliesthat{@codecompare(x,y)}mustthrowanexceptionifandonly*if{@codecompare(y,x)}throwsanexception.)<p>*实现者必须确保所有x和y的sgn(compare(x,y))==-sgn(compare(y,x))。*(这意味着compare(x,y)必须在且仅在compare(y,x)引发异常时引发异常。)**Theimplementormustalsoensurethattherelationistransitive:*{@code((compare(x,y)>0)&&(compare(y,z)>0))}implies*{@codecompare(x,z)>0}.<p>*实现者还必须确保关系是可传递的:*((compare(x,y)>0)&&(compare(y,z)>0))意味着compare(x,z)>0*****Finally,theimplementormustensurethat{@codecompare(x,y)==0}*impliesthat{@codesgn(compare(x,z))==sgn(compare(y,z))}forall*{@codez}.<p>*最后,实现者必须确保compare(x,y)==0意味着所有z的*sgn(compare(x,z))==sgn(compare(y,z))****Itisgenerallythecase,but<i>not</i>strictlyrequiredthat*{@code(compare(x,y)==0)==(x.equals(y))}.Generallyspeaking,*anycomparatorthatviolatesthisconditionshouldclearlyindicate*thisfact.Therecommendedlanguageis"Note:thiscomparator*imposesorderingsthatareinconsistentwithequals."<p>*通常是这样的,但不是严格要求(compare(x,y)==0)==(x.equals(y))。*⼀般来说,任何违反这⼀条件的⽐较国都应明确指出这⼀事实。*推荐的语⾔是“注意:这个⽐较器强加的顺序与equals不⼀致。”***Intheforegoingdescription,thenotation*{@codesgn(}<i>expression</i>{@code)}designatesthemathematical*<i>signum</i>function,whichisdefinedtoreturnoneof{@code-1},*{@code0},or{@code1}accordingtowhetherthevalueof*<i>expression</i>isnegative,zero,orpositive,respectively.*在前⾯的描述中,符号sgn(expression)指定数学符号函数,*该函数被定义为根据表达式的值是负、零还是正分别返回-1、0或1中的⼀个。**@paramo1thefirstobjecttobecompared.第⼀个要⽐较的对象。*@paramo2thesecondobjecttobecompared.要⽐较的第⼆个对象。*@returnanegativeinteger,zero,orapositiveintegerasthe**firstargumentislessthan,equalto,orgreaterthanthesecond.作为第⼀个参数的负整数、零或正整数⼩于、等于或⼤于第⼆个参数。*@throwsNullPointerExceptionifanargumentisnullandthis*comparatordoesnotpermitnullarguments*comparatordoesnotpermitnullarguments*@throwsClassCastExceptionifthearguments'typespreventthemfrom*beingcomparedbythiscomparator.如果参数的类型阻⽌此⽐较器对其进⾏⽐较。*/intcompare(To1,To2);/***Indicateswhethersomeotherobjectis"equalto"this*comparator.Thismethodmustobeythegeneralcontractof*{@linkObject#equals(Object)}.Additionally,thismethodcanreturn*{@codetrue}<i>only</i>ifthespecifiedobjectisalsoacomparator*anditimposesthesameorderingasthiscomparator.Thus,*{@codecomp1.equals(comp2)}impliesthat{@codesgn(pare(o1,*o2))==sgn(pare(o1,o2))}foreveryobjectreference*{@codeo1}and{@codeo2}.<p>*指⽰其他对象是否等于此⽐较器。*此⽅法必须遵守Object#equals(Object)的⼀般约定。*此外,仅当指定的对象也是⼀个⽐较器并且它施加了与此⽐较器相同的顺序时,*此⽅法才能返回true。因此,comp1.equals(comp2)意味着对于每个对象引⽤o1和o2都有*sgn(pare(o1,o2))==sgn(pare(o1,o2))*。****Notethatitis<i>always</i>safe<i>not</i>tooverride*{@codeObject.equals(Object)}.However,overridingthismethodmay,*insomecases,improveperformancebyallowingprogramstodetermine*thattwodistinctcomparatorsimposethesameorder.*注意,不重写Object.equals(Object)总是安全的。*但是,在某些情况下,*重写此⽅法可以通过允许程序确定两个不同的⽐较器施加相同的顺序来提⾼性能。**@paramobjthereferenceobjectwithwhichtocompare.*@return{@codetrue}onlyifthespecifiedobjectisalso**acomparatoranditimposesthesameorderingasthiscomparator.*@seeObject#equals(Object)*@seeObject#hashCode()*/booleanequals(Objectobj);/***Returnsacomparatorthatimposesthereverseorderingofthis*comparator.*返回⼀个⽐较器,该⽐较器对该⽐较器进⾏反向排序。**@returnacomparatorthatimposesthereverseorderingofthis*comparator.⼀种⽐较器,对该⽐较器进⾏反向排序*@since1.8*/defaultComparator<T>reversed(){returnCollections.reverseOrder(this);}/***Returnsalexicographic-ordercomparatorwithanothercomparator.*Ifthis{@codeComparator}considerstwoelementsequal,i.e.*{@codecompare(a,b)==0},{@codeother}isusedtodeterminetheorder.*返回⼀个字典顺序⽐较器和另⼀个⽐较器。如果此Comparator认为两个元素相等,*即compare(a,b)==0,则使⽤other确定顺序。**<p>Thereturnedcomparatorisserializableifthespecifiedcomparator*isalsoserializable.*如果指定的⽐较器也可序列化,则返回的⽐较器可序列化。**@apiNote*@apiNote*Forexample,tosortacollectionof{@codeString}basedonthelength*andthencase-insensitivenaturalordering,thecomparatorcanbe*composedusingfollowingcode,*例如,要根据长度对String集合进⾏排序,*然后进⾏不区分⼤⼩写的⾃然排序,可以使⽤以下代码组合⽐较器:,**<pre>{@code**Comparator<String>cmp=CparingInt(String::length).thenComparing(String.CASE_INSENSITIVE_ORDER);*}</pre>**@paramothertheothercomparatortobeusedwhenthiscomparator*comparestwoobjectsthatareequal.当此⽐较器⽐较两个相等的对象时要使⽤的另⼀个⽐较器。*@returnalexicographic-ordercomparatorcomposedofthisandthentheothercomparator由这个⽐较器和另⼀个⽐较器组成的字典顺序⽐较器**@throwsNullPointerExceptioniftheargumentisnull.*@since1.8*/defaultComparator<T>thenComparing(Comparator<?superT>other){Objects.requireNonNull(other);return(Comparator<T>&Serializable)(c1,c2)->{intres=compare(c1,c2);return(res!=0)?res:pare(c1,c2);};}/***Returnsalexicographic-ordercomparatorwithafunctionthat*extractsakeytobecomparedwiththegiven{@codeComparator}.*返回字典顺序⽐较器,该⽐较器具有⼀个函数,*该函数提取要与给定comparator进⾏⽐较的键。**@implSpecThisdefaultimplementationbehavesasif{@code***thenComparing(comparing(keyExtractor,cmp))}.这个默认实现的⾏为就像thenComparing(comparing(keyExtractor,cmp))*@param<U>thetypeofthesortkey*@paramkeyExtractorthefunctionusedtoextractthesortkey*@paramkeyComparatorthe{@codeComparator}usedtocomparethesortkey*@returnalexicographic-ordercomparatorcomposedofthiscomparator****andthencomparingonthekeyextractedbythekeyExtractorfunction⼀种字典顺序⽐较器,由该⽐较器组成,然后对keyExtractor函数提取的密钥进⾏⽐较*@throwsNullPointerExceptionifeitherargumentisnull.如果任⼀参数为空。*@see#comparing(Function,Comparator)*@see#thenComparing(Comparator)*@since1.8*/default<U>Comparator<T>thenComparing(Function<?superT,?extendsU>keyExtractor,Comparator<?superU>keyComparator){returnthenComparing(comparing(keyExtractor,keyComparator));}/***Returnsalexicographic-ordercomparatorwithafunctionthat*extractsa{@codeComparable}sortkey.*返回⼀个字典顺序⽐较器,该⽐较器带有⼀个提取compariable排序键的函数。**@implSpecThisdefaultimplementationbehavesasif{@code***thenComparing(comparing(keyExtractor))}.此默认实现的⾏为就像thenComparing(comparing(keyExtractor))**@param<U>thetypeofthe{@linkComparable}sortkey可⽐较的排序键的类型*@paramkeyExtractorthefunctionusedtoextractthe{@link*Comparable}sortkey⽤于提取可⽐较排序键的函数*@returnalexicographic-ordercomparatorcomposedofthisandthenthe****{@linkComparable}sortkey.⼀种字典顺序⽐较器,由这个键和Comparable排序键组成。*@throwsNullPointerExceptioniftheargumentisnull.*@see#comparing(Function)*@see#thenComparing(Comparator)*@since1.8*/default<UextendsComparable<?superU>>Comparator<T>thenComparing(Function<?superT,?extendsU>keyExtractor){returnthenComparing(comparing(keyExtractor));}/***Returnsalexicographic-ordercomparatorwithafunctionthat*extractsan{@codeint}sortkey.*返回字典顺序⽐较器,该⽐较器带有⼀个提取int排序键的函数。**@implSpecThisdefaultimplementationbehavesasif{@code***thenComparing(comparingInt(keyExtractor))}.这个默认实现的⾏为就像thenComparing(comparingInt(keyExtractor))*@paramkeyExtractorthefunctionusedtoextracttheintegersortkey⽤于提取整数排序键的函数*@returnalexicographic-ordercomparatorcomposedofthisandthenthe*{@codeint}sortkey⼀种字典顺序⽐较器,由这个键和int排序键组成*@throwsNullPointerExceptioniftheargumentisnull.*@see#comparingInt(ToIntFunction)*@see#thenComparing(Comparator)*@since1.8*/defaultComparator<T>thenComparingInt(ToIntFunction<?superT>keyExtractor){returnthenComparing(comparingInt(keyExtractor));}/***Returnsalexicographic-ordercomparatorwithafunctionthat*extractsa{@codelong}sortkey.*返回字典顺序⽐较器,该⽐较器带有⼀个提取long排序键的函数。**@implSpecThisdefaultimplementationbehavesasif{@code***thenComparing(comparingLong(keyExtractor))}.这个默认实现的⾏为就像thenComparing(comparingLong(keyExtractor))*@paramkeyExtractorthefunctionusedtoextractthelongsortkey⽤于提取long排序键的函数*@returnalexicographic-ordercomparatorcomposedofthisandthenthe*{@codelong}sortkey⼀种字典顺序⽐较器,由这个键和long排序键组成*@throwsNullPointerExceptioniftheargumentisnull.*@see#comparingLong(ToLongFunction)*@see#thenComparing(Comparator)*@since1.8*/defaultComparator<T>thenComparingLong(ToLongFunction<?superT>keyExtractor){returnthenComparing(comparingLong(keyExtractor));}/***Returnsalexicographic-ordercomparatorwithafunctionthat*extractsa{@codedouble}sortkey.*返回字典顺序⽐较器,该⽐较器带有⼀个提取double排序键的函数。*返回字典顺序⽐较器,该⽐较器带有⼀个提取double排序键的函数。**@implSpecThisdefaultimplementationbehavesasif{@code****thenComparing(comparingDouble(keyExtractor))}.这个默认实现的⾏为就像thenComparing(comparingDouble(keyExtractor))*@paramkeyExtractorthefunctionusedtoextractthedoublesortkey⽤于提取double排序键的函数*@returnalexicographic-ordercomparatorcomposedofthisandthenthe*{@codedouble}sortkey⼀种字典顺序⽐较器,由这个键和double排序键组成*@throwsNullPointerExceptioniftheargumentisnull.*@see#comparingDouble(ToDoubleFunction)*@see#thenComparing(Comparator)*@since1.8*/defaultComparator<T>thenComparingDouble(ToDoubleFunction<?superT>keyExtractor){returnthenComparing(comparingDouble(keyExtractor));}/***Returnsacomparatorthatimposesthereverseofthe<em>natural*ordering</em>.*返回与⾃然顺序相反的⽐较器**<p>Thereturnedcomparatorisserializableandthrows{@link*NullPointerException}whencomparing{@codenull}.*返回的⽐较器可序列化,并在⽐较null时抛出NullPointerException**@param<T>the{@linkComparable}typeofelementtobecompared待⽐较元素的可⽐较类型*@returnacomparatorthatimposesthereverseofthe<i>natural*ordering</i>on{@codeComparable}objects.*@seeComparable*@since1.8*/publicstatic<TextendsComparable<?superT>>Comparator<T>reverseOrder(){returnCollections.reverseOrder();}/***Returnsacomparatorthatcompares{@linkComparable}objectsinnatural*order.*返回按⾃然顺序⽐较Comparable对象的⽐较器。**<p>Thereturnedcomparatorisserializableandthrows{@link*NullPointerException}whencomparing{@codenull}.*返回的⽐较器可序列化,并在⽐较null时抛出NullPointerException**@param<T>the{@linkComparable}typeofelementtobecompared要⽐较的元素的Comparable类型*@returnacomparatorthatimposesthe<i>naturalordering</i>on{@code*Comparable}objects.对compariable对象施加⾃然排序的⽐较器。*@seeComparable*@since1.8*/@SuppressWarnings("unchecked")publicstatic<TextendsComparable<?superT>>Comparator<T>naturalOrder(){return(Comparator<T>)Comparators.NaturalOrderComparator.INSTANCE;}/***Returnsanull-friendlycomparatorthatconsiders{@codenull}tobe*lessthannon-null.Whenbothare{@codenull},theyareconsidered*equal.Ifbotharenon-null,thespecified{@codeComparator}isused*todeterminetheorder.Ifthespecifiedcomparatoris{@codenull},*thenthereturnedcomparatorconsidersallnon-nullvaluestobeequal.*返回⼀个空友好的⽐较器,该⽐较器认为null⼩于⾮空。*当两者都是null时,它们被视为相等。*当两者都是null时,它们被视为相等。*如果两者都⾮空,则使⽤指定的Comparator确定顺序。*如果指定的⽐较器为null,则返回的⽐较器将认为所有⾮null值相等。**<p>Thereturnedcomparatorisserializableifthespecifiedcomparator*isserializable.*如果指定的⽐较器可序列化,则返回的⽐较器可序列化。**@param<T>thetypeoftheelementstobecompared*@paramcomparatora{@codeComparator}forcomparingnon-nullvalues*@returnacomparatorthatconsiders{@codenull}tobelessthan***non-null,andcomparesnon-nullobjectswiththesupplied{@codeComparator}.⼀种⽐较器,它认为null⼩于⾮null,并将⾮null对象与提供的comparator进⾏⽐较。*@since1.8*/publicstatic<T>Comparator<T>nullsFirst(Comparator<?superT>comparator){returnnewComparators.NullComparator<>(true,comparator);}/***Returnsanull-friendlycomparatorthatconsiders{@codenull}tobe*greaterthannon-null.Whenbothare{@codenull},theyareconsidered*equal.Ifbotharenon-null,thespecified{@codeComparator}isused*todeterminetheorder.Ifthespecifiedcomparatoris{@codenull},*thenthereturnedcomparatorconsidersallnon-nullvaluestobeequal.*返回⼀个空友好的⽐较器,该⽐较器认为null⼤于⾮空。*当两者都是null时,它们被视为相等。如果两者都⾮空,*则使⽤指定的Comparator确定顺序。如果指定的⽐较器为null,*则返回的⽐较器将认为所有⾮null值相等。**<p>Thereturnedcomparatorisserializableifthespecifiedcomparator*isserializable.*如果指定的⽐较器可序列化,则返回的⽐较器可序列化。**@param<T>thetypeoftheelementstobecompared*@paramcomparatora{@codeComparator}forcomparingnon-nullvalues*@returnacomparatorthatconsiders{@codenull}tobegreaterthan*****non-null,andcomparesnon-nullobjectswiththesupplied{@codeComparator}.⼀种⽐较器,它认为null⼤于⾮null,并将⾮null对象与提供的comparator进⾏⽐较。*@since1.8*/publicstatic<T>Comparator<T>nullsLast(Comparator<?superT>comparator){returnnewComparators.NullComparator<>(false,comparator);}/***Acceptsafunctionthatextractsasortkeyfromatype{@codeT},and*returnsa{@codeComparator<T>}thatcomparesbythatsortkeyusing*thespecified{@linkComparator}.*接受⼀个函数,该函数从类型T中提取排序键,*并返回⼀个Comparator<T>,使⽤指定的Comparator按该排序键进⾏⽐较。**<p>Thereturnedcomparatorisserializableifthespecifiedfunction*andcomparatorarebothserializable.*如果指定的函数和⽐较器都可序列化,则返回的⽐较器可序列化。**@apiNote*Forexample,toobtaina{@codeComparator}thatcompares{@code*Person}objectsbytheirlastnameignoringcasedifferences,*例如,要获得⼀个Comparator,*它根据Person对象的姓⽒⽐较Person对象,忽略⼤⼩写差异,***<pre>{@code*Comparator<Person>cmp=Cparing(*Person::getLastName,*String.CASE_INSENSITIVE_ORDER);*}</pre>**@param<T>thetypeofelementtobecompared*@param<U>thetypeofthesortkey*@paramkeyExtractorthefunctionusedtoextractthesortkey⽤于提取排序键的函数*@paramkeyComparatorthe{@codeComparator}usedtocomparethesortkey⽤于⽐较排序键的Comparator*@returnacomparatorthatcomparesbyanextractedkeyusingthe*specified{@codeComparator}⼀种⽐较器,使⽤指定的comparator通过提取的键进⾏⽐较*@throwsNullPointerExceptionifeitherargumentisnull*@since1.8*/publicstatic<T,U>Comparator<T>comparing(Function<?superT,?extendsU>keyExtractor,Comparator<?superU>keyComparator){Objects.requireNonNull(keyExtractor);Objects.requireNonNull(keyComparator);return(Comparator<T>&Serializable)(c1,c2)->keyCpare(keyExtractor.apply(c1),keyExtractor.apply(c2));}/***Acceptsafunctionthatextractsa{@linkjava.lang.Comparable*Comparable}sortkeyfromatype{@codeT},andreturnsa{@code*Comparator<T>}thatcomparesbythatsortkey.*接受⼀个函数,该函数从类型T中提取java.lang.Comparable排序键,*并返回⼀个Comparator<T>按该排序键进⾏⽐较。**<p>Thereturnedcomparatorisserializableifthespecifiedfunction*isalsoserializable.*如果指定的函数也可序列化,则返回的⽐较器可序列化。**@apiNote*Forexample,toobtaina{@codeComparator}thatcompares{@code*Person}objectsbytheirlastname,*例如,要获得⼀个Comparator,它根据Person对象的姓⽒对其进⾏⽐较,**<pre>{@code*Comparator<Person>byLastName=Cparing(Person::getLastName);*}</pre>**@param<T>thetypeofelementtobecompared*@param<U>thetypeofthe{@codeComparable}sortkey*@paramkeyExtractorthefunctionusedtoextractthe{@link*Comparable}sortkey*@returnacomparatorthatcomparesbyanextractedkey*@throwsNullPointerExceptioniftheargumentisnull*@since1.8*/publicstatic<T,UextendsComparable<?superU>>Comparator<T>comparing(Function<?superT,?extendsU>keyExtractor){Objects.requireNonNull(keyEx
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 家庭成熟期保险规划
- 2025年学校元旦宣传模板
- 糖尿病护理亮点课件
- 材料力学第10章构件的疲劳强度
- 服务业沟通培训
- 物业小区虫害防制案例客户表
- 课件背景图教学课件
- 网络课件教学课件
- 颈枕融合术及护理
- 激素药膏与皮肤病
- 学生随到随进校园制度
- DL∕T 5161.5-2018 电气装置安装工程质量检验及评定规程 第5部分:电缆线路施工质量检验
- 养老产业投资合同
- 《沥青路面智能化摊铺压实技术规范》
- 浅谈如何提高小学生计算能力讲座课件
- 非物质文化遗产介绍-缠花
- 2024年北京市行政职业能力测验题库及完整答案一套
- 食品发酵与酿造习题库(附参考答案)
- 万物之理-爱因斯坦之梦智慧树知到期末考试答案章节答案2024年中国海洋大学
- 第二章-中国酒的起源与中国酒文化的发展
- 2023-华为-Datacom-HCIE-真题题库
评论
0/150
提交评论