什么是 keyExtractor 参数

huangapple go评论111阅读模式
英文:

What is a keyExtractor parameter

问题

我正在学习Comparator接口,对它的静态Comparator.comparing()方法感到困惑。关于Comparator.comparing()方法的参数,以及它如何使用方法引用。当我查阅文档时,文档称它有一个"keyExtractor参数"。你能解释一下让我感到困惑的是什么吗?

英文:

I'm learning the Comparator interface and I'm confused with it's static Comparator.comparing() method. About Comparator.comparing() method's parameters and how it can use method references. When I looked at the documentations it says that that it has a "keyExtractor parameter". Can you explain what is confusing me?

答案1

得分: 4

来自 Comparator#comparing(Function) 文档:

> 接受从类型 T 中提取可比较排序键的函数,并返回一个比较器 Comparator<T>,该比较器通过该排序键进行比较。

这样你就可以基于对象的属性来进行比较。同样的文档给出了一个示例:

> API 注释:
>
> 例如,要获得一个通过姓氏比较 Person 对象的比较器,
>
> java > Comparator<Person> byLastName = Comparator.comparing(Person::getLastName); >

当你执行:

Person p1 = ...;
Person p2 = ...;
int result = byLastName.compare(p1, p2);

给定的键提取器将从每个 Person 中提取姓氏值,以便比较这些值,而不是“直接”比较 Person 对象。如果键不是 Comparable,那么你可以使用重载版本,该版本允许你为比较提取的键值指定一个比较器。


上述的 byLastName 比较器与以下相同:

public class ByLastNameComparator implements Comparator<Person> {

  @Override
  public int compare(Person p1, Person p2) {
    return p1.getLastName().compareTo(p2.getLastName());
  }
}

其中对 p1.getLastName()p2.getLastName() 的调用将成为键提取器 Function 的实现。

英文:

From the documentation of Comparator#comparing(Function):

>Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.

It's so you can compare objects based on a property of those objects. The same documentation gives an example:

>API Note:
>
>For example, to obtain a Comparator that compares Person objects by their last name,
>
>java
>Comparator<Person> byLastName = Comparator.comparing(Person::getLastName);
>

When you do:

Person p1 = ...;
Person p2 = ...;
int result = byLastName.compare(p1, p2);

The given key extractor will extract the last name values from each Person in order to compare those values rather than the Person objects "directly". If the key is not Comparable then you can use the overload which lets you specify a Comparator for comparing the extracted key values.


The above byLastName comparator would be the same as:

public class ByLastNameComparator implements Comparator<Person> {

  @Override
  public int compare(Person p1, Person p2) {
    return p1.getLastName().compareTo(p2.getLastName());
  }
}

Where the calls to p1.getLastName() and p2.getLastName() would be the key extractor Function implementation.

答案2

得分: 2

Comparator.comparing()keyExtractor - 用于提取可比较排序键的函数

这意味着它是一个函数,用于找出给定元素的哪些参数应该被用来评估每个元素之间的位置。

想象一个类似这样的类

class Elt {
    int a, b;
    float c;
    public int getA() { return a; }
    public int getB() { return b; }
    public float getC() { return c; }
}

您可以使用几个键来进行比较,如 abc

public static void main(String[] arg) {
    List<Elt> res = Arrays.asList(new Elt(), new Elt());
    res.sort(Comparator.comparing(Elt::getA)); // elt -> elt.getA()
    res.sort(Comparator.comparing(Elt::getB)); // elt -> elt.getB()
    res.sort(Comparator.comparing(Elt::getC)); // elt -> elt.getC()
}

Comparator 还允许使用 thenComparing 链接 keyExtractor

// 区分具有相同 'c' 的元素,例如
res.sort(Comparator.comparing(Elt::getC).thenComparing(Elt::getA)); 
英文:

> From Comparator.comparing() : keyExtractor - the function used to extract the Comparable sort key

This means, it is a function to find out which parameters of the given elements, should be taken to evaluate the position between each elements

Imagine a class like

class Elt {
    int a, b;
    float c;
    public int getA() { return a; }
    public int getB() { return b; }
    public float getC() { return c; }
}

You can use several keys to compare them, a, b or c

public static void main(String[] arg) {
    List&lt;Elt&gt; res = Arrays.asList(new Elt(), new Elt());
    res.sort(Comparator.comparing(Elt::getA)); // elt -&gt; elt.getA()
    res.sort(Comparator.comparing(Elt::getB)); // elt -&gt; elt.getB()
    res.sort(Comparator.comparing(Elt::getC)); // elt -&gt; elt.getC()
}

Comparator also allows to chain keyExtractor using thenComparing

// differentiate element with same &#39;c&#39; for ex
res.sort(Comparator.comparing(Elt::getC).thenComparing(Elt::getA)); 

huangapple
  • 本文由 发表于 2020年3月15日 23:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定