Home > Back-end >  Comparator.com paring () the parameters of the problem
Comparator.com paring () the parameters of the problem

Time:10-03


Source of Comparator.com paring () needs to receive a Function parameter of type:

Public static & lt; T, U extends Comparable<? Super U> Comparator Comparing (
Function<? Super T,? Extends U> KeyExtractor)
{
Objects. RequireNonNull (keyExtractor);
Return (Comparator & The Serializable)
(c1, c2) - & gt; KeyExtractor. Apply (c1). CompareTo (keyExtractor. Apply (c2));
}

Why at the time when using, can be introduced to: Supplier type parameters, the following

Supplier ColorStr=apple: : getColor;
Apples. Sort (Comparator.com paring Apple: : getColor ());


Apple: : getColor return value types: Supplier types, i.e., they can out of the incoming comparing method, and the Function and: Supplier not implemented in the source and inheritance relationships

CodePudding user response:

There is a problem
Collectors. ToList () method of source:

Public static & lt; T>
Collector ToList () {
Return new CollectorImpl<> ((Supplier & gt;) ArrayList: : new, List: : add,
(left, right) - & gt; {left. AddAll (right); Return the left; },
CH_ID);
}

New CollectorImpl is the second parameter of the incoming List: : add

But function definition is like this:

CollectorImpl (Supplier : supplier,
BiConsumer An accumulator,
BinaryOperator
Combiner,
Set Characteristics) {
This (: supplier, accumulator, combiner, castingIdentity (), characteristics);
}

BiConsumer Accumulator, how to receive the List: : add, normally speak List: : add is a typical BiPredicate type

CodePudding user response:

I don't understand why, so also in mybatisplus LambdaQueryWrapper, they tried they not do

CodePudding user response:

Supplier ColorStr=apple: : getColor;
Apples. Sort (Comparator.com paring Apple: : getColor ());

Obviously not preach entered colorStr: Supplier type
  • Related