java comparator性能,Java 8 Comparator比较不链

Let's say I have a Pair class

public class Pair

{

public P p;

public Q q;

public Pair(P p, Q q) {

this.p = p;

this.q = q;

}

public int firstValue() {

return ((Number)p).intValue();

}

public int secondValue() {

return ((Number)q).intValue();

}

}

And I wish to sort it, first by first value, then by second value. Now' if I do this

List> pairList = new ArrayList<>();

pairList.add(new Pair<>(1, 5));

pairList.add(new Pair<>(2, 2));

pairList.add(new Pair<>(2, 22));

pairList.add(new Pair<>(1, 22));

pairList.sort(Comparator.comparing(Pair::firstValue));

Everything works well and good, the list is sorted by first values of pair, but if I do this

pairList.sort(Comparator.comparing(Pair::firstValue).thenComparing(Pair::secondValue));

It fails with error

Error:(24, 38) java: incompatible types: cannot infer type-variable(s) T,U

(argument mismatch; invalid method reference

method firstValue in class DataStructures.Pair

cannot be applied to given types

required: no arguments

found: java.lang.Object

reason: actual and formal argument lists differ in length)

Ok,so it might not be able to infer the arguments, so if I do this

pairList.sort(Comparator.comparing(Pair::firstValue)

.thenComparing(Pair::secondValue));

It fails with error

Error:(24, 39) java: invalid method reference

non-static method firstValue() cannot be referenced from a static context

Why does it work for comparing() and not for comparing().thenComparing() ?

解决方案

The error seems to be related to Pair's generic parameters. One workaround it to use an explicit type, as you've attempted:

pairList.sort(Comparator.comparingInt(Pair::firstValue).thenComparingInt(Pair::secondValue));

// ^^^^^^

Note the comparingInt() which reduces the number of parameters you need to specify, and improves performance by avoiding boxing.

Another solution is to parameterize the type reference:

pairList.sort(Comparator.comparingInt(Pair,?>::firstValue).thenComparingInt(Pair::secondValue));

// ^^^^^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值