java collections max,Java泛型:Collections.max()签名和比较器

I understand the get and put principle for collections: if a method takes in a collection that it will write a type T to, the parameter has to be Collection super T>, whereas if it will read a type T from, the parameter has to be Collection extends T>.

But could someone please explain the Collections.max() signature:

public static T max(Collection extends T> coll,

Comparator super T> comp)

In particular why is it Comparator super T> instead of Comparator extends T> ?

解决方案

Josh Bloch's mnemonic PECS is useful here. It stands for:

Producer extends, Consumer super

This means that when a parameterized type being passed to a method will produce instances of T (they will be retrieved from it in some way), ? extends T should be used, since any instance of a subclass of T is also a T.

When a parameterized type being passed to a method will consume instances of T (they will be passed to it to do something), ? super T should be used because an instance of T can legally be passed to any method that accepts some supertype of T. A Comparator could be used on a Collection, for example. ? extends T would not work, because a Comparator could not operate on a Collection.

Edit:

To clarify a little more on get/put (produce/consume):

public T something();

^

The above is a method that produces T.

public void something(T t);

^

The above is a method that consumes T.

"Producer extends, Consumer super" applies to how the method a parameterized object is being passed to is going to be using that object. In the case of Collections.max(), items will be retrieved from the Collection, so it is a producer. Those items will be passed as arguments to the method on Comparator, so it is a consumer.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值