Java比较器链 [英] Chain of comparators in java

查看:72
本文介绍了Java比较器链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在接口上阅读 Java教程 Card(纸牌)上的示例我试图理解默认接口中的方法.这是链接,在现有接口中集成默认方法"部分.现在,在最后一节中,他们首先按等级然后按西装对卡片进行排序.给出了以下逻辑.假定已定义了所使用的任何接口,函数或类,并且sort函数采用Comparator

Reading the Java Tutorial by Oracle on interfaces which gives a example on Card (Playing cards) I was trying to understand the default methods in interfaces. Here's the link, section "Integrating default methods in existing interfaces". Now in the last section they sorted the Cards first by rank and then by suits. Following logics have been given. Assume that whatever interfaces, functions or classes that are used have been defined and sort function takes a Comparator

逻辑1:

package defaultmethods;

import java.util.*;
import java.util.stream.*;
import java.lang.*;

public class SortByRankThenSuit implements Comparator<Card> {
    public int compare(Card firstCard, Card secondCard) {
        int compVal =
            firstCard.getRank().value() - secondCard.getRank().value();
        if (compVal != 0)
            return compVal;
        else
            return firstCard.getSuit().value() - secondCard.getSuit().value(); 
    }
}

逻辑2:

myDeck.sort(
    Comparator
        .comparing(Card::getRank)
        .thenComparing(Comparator.comparing(Card::getSuit)));

现在我在理解第二种逻辑时遇到了一些问题.我阅读了Java 1.8中包含的比较器接口和新的静态方法.现在,我了解了类似myDeck.sort(Comparator.comparing(Card::getRank))的内容,该内容按排名排序,但在阅读了

Now I am having some problems in understanding the second logic. I read the comparator interfaces and the new static methods which have been included in Java 1.8 . Now I understand something like this myDeck.sort(Comparator.comparing(Card::getRank)) which sorts by rank but after reading the documentation for thenComparing , I am unable to understand how thenComparing returns a Comparator which achieves the above Logic 1. Does it internally build something like the if-else construct as specified in Logic 1 ?

推荐答案

是的,它在内部创建类似的东西,只是具有更多的中间lambda.假设您的getRankgetSuit方法返回了一些类似的类RankSuit的实例,那么在您的情况下,您实际上拥有:

Yes, it creates internally something similar, just with more intermediate lambdas. Assuming that your getRank and getSuit methods return some instances of comparable classes Rank and Suit, in your case you effectively have:

Function<Card, Rank> toRank = Card::getRank;
Comparator<Card> comp1 = (a, b) -> toRank.apply(a).compareTo(toRank.apply(b));
Function<Card, Suit> toSuit = Card::getSuit;
Comparator<Card> comp2 = (a, b) -> toSuit.apply(a).compareTo(toSuit.apply(b));
Comparator<Card> result = (a, b) -> {
  int res = comp1.compare(a, b);
  return res != 0 ? res : comp2.compare(a, b);
};

因此,在内联之后(可能由JIT编译器执行),您可能会遇到类似这样的情况:

So after inlining (which might be performed by JIT-compiler) you may have something like this:

Comparator<Card> result = (a, b) -> {
  int res = a.getRank().compareTo(b.getRank());
  return res != 0 ? res : a.getSuit().compareTo(b.getSuit());
};

请注意,您可以使用更简单的版本:

Note that you can use the simpler version:

myDeck.sort(
    Comparator
        .comparing(Card::getRank)
        .thenComparing(Card::getSuit));

这篇关于Java比较器链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆