Inconsistency in Java 8 method signatures

后端 未结 3 467
名媛妹妹
名媛妹妹 2021-02-08 09:46

Java 8 has given us new methods with really long signatures like this:

static > Collector toMap(
    Function&l         


        
3条回答
  •  旧时难觅i
    2021-02-08 10:25

    the BinaryOperator mergeFunction needs to take Us from an input source and put them into another consumer.

    Due to the Get and Put Principle, the type has to be exactly the same. No wild cards.

    The get-put principle, as stated in Naftalin and Wadler's fine book on generics, Java Generics and Collections says:

    Use an extends wildcard when you only get values out of a structure, use a super wildcard when you only put values into a structure, and don't use a wildcard when you do both.

    Therefore it can't beBiFunction mergefunction because we are doing get and put operations. Therefore the input and result type must be identical.

    see these other links for more about Get and Put:

    Explanation of the get-put principle (SO question)

    http://www.ibm.com/developerworks/library/j-jtp07018/

    EDIT

    As Gab points out, the Get and Put principle is also known by the Acronym PECS for "Producer Extends Consumer Super"

    What is PECS (Producer Extends Consumer Super)?

提交回复
热议问题