Should 'Comparable' be a 'Functional interface'?

前端 未结 4 1081
星月不相逢
星月不相逢 2020-12-08 14:37

The definition of a functional interface is \"A functional interface is an interface that has just one abstract method (aside from the methods of Object ), and thus represen

4条回答
  •  猫巷女王i
    2020-12-08 15:42

    Well, asides from the discussion how usefull the informative annotation @FunctionalInterface is (and I am happy Java 8 does not require it for lambdas).

    Comparable is typically a property of a type and therefore not a good candidate for a functional interface. It is explicitly described as the natural ordering and does not take the two this/that arguments. So this property makes it unlikely any method would operate on a lambda (similliar argument is applicable for nearly all -able interfaces).

    So, the collection designers use a second interface for that task: Comparator, and for that a lambda implementing it is a very natural choice.

提交回复
热议问题