Generic method reference type specifying before/after :: operator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
17
down vote

favorite
2












What is the difference between the following method references,



BiPredicate<List<String>,String> contains1 = List<String>::contains;

BiPredicate<List<String>,String> contains2 = List::<String>contains;

BiPredicate<List<String>,String> contains3 = List<String>::<String>contains;


Do the cases have special names? Is there any example similar to the usage?







share|improve this question


















  • 7




    Good question...
    – ernest_k
    Aug 10 at 12:35










  • Related: stackoverflow.com/questions/31245127/…. Seems the first syntax specifies type argument for List, whereas the second specifies type argument for contains (unnecessary in this case because the method is not generic)
    – ernest_k
    Aug 10 at 12:44






  • 3




    And, of course, BiPredicate<List<String>,String> contains1 = List<String>::<String>contains;.
    – Andy Turner
    Aug 10 at 12:45






  • 2




    As a related side-note, and a partial explanation: it's legal to supply type arguments to a non-generic method, such as list.<Number>contains("foo"). They're just ignored. (As for why the JLS authors chose to allow it, though, I don't know.)
    – Radiodef
    Aug 10 at 16:53







  • 1




    @snr Why is it legal to supply type arguments to a non-generic method? You can find the answer here.
    – Oleksandr
    Aug 10 at 20:03















up vote
17
down vote

favorite
2












What is the difference between the following method references,



BiPredicate<List<String>,String> contains1 = List<String>::contains;

BiPredicate<List<String>,String> contains2 = List::<String>contains;

BiPredicate<List<String>,String> contains3 = List<String>::<String>contains;


Do the cases have special names? Is there any example similar to the usage?







share|improve this question


















  • 7




    Good question...
    – ernest_k
    Aug 10 at 12:35










  • Related: stackoverflow.com/questions/31245127/…. Seems the first syntax specifies type argument for List, whereas the second specifies type argument for contains (unnecessary in this case because the method is not generic)
    – ernest_k
    Aug 10 at 12:44






  • 3




    And, of course, BiPredicate<List<String>,String> contains1 = List<String>::<String>contains;.
    – Andy Turner
    Aug 10 at 12:45






  • 2




    As a related side-note, and a partial explanation: it's legal to supply type arguments to a non-generic method, such as list.<Number>contains("foo"). They're just ignored. (As for why the JLS authors chose to allow it, though, I don't know.)
    – Radiodef
    Aug 10 at 16:53







  • 1




    @snr Why is it legal to supply type arguments to a non-generic method? You can find the answer here.
    – Oleksandr
    Aug 10 at 20:03













up vote
17
down vote

favorite
2









up vote
17
down vote

favorite
2






2





What is the difference between the following method references,



BiPredicate<List<String>,String> contains1 = List<String>::contains;

BiPredicate<List<String>,String> contains2 = List::<String>contains;

BiPredicate<List<String>,String> contains3 = List<String>::<String>contains;


Do the cases have special names? Is there any example similar to the usage?







share|improve this question














What is the difference between the following method references,



BiPredicate<List<String>,String> contains1 = List<String>::contains;

BiPredicate<List<String>,String> contains2 = List::<String>contains;

BiPredicate<List<String>,String> contains3 = List<String>::<String>contains;


Do the cases have special names? Is there any example similar to the usage?









share|improve this question













share|improve this question




share|improve this question








edited Aug 10 at 14:51

























asked Aug 10 at 12:33









snr

1




1







  • 7




    Good question...
    – ernest_k
    Aug 10 at 12:35










  • Related: stackoverflow.com/questions/31245127/…. Seems the first syntax specifies type argument for List, whereas the second specifies type argument for contains (unnecessary in this case because the method is not generic)
    – ernest_k
    Aug 10 at 12:44






  • 3




    And, of course, BiPredicate<List<String>,String> contains1 = List<String>::<String>contains;.
    – Andy Turner
    Aug 10 at 12:45






  • 2




    As a related side-note, and a partial explanation: it's legal to supply type arguments to a non-generic method, such as list.<Number>contains("foo"). They're just ignored. (As for why the JLS authors chose to allow it, though, I don't know.)
    – Radiodef
    Aug 10 at 16:53







  • 1




    @snr Why is it legal to supply type arguments to a non-generic method? You can find the answer here.
    – Oleksandr
    Aug 10 at 20:03













  • 7




    Good question...
    – ernest_k
    Aug 10 at 12:35










  • Related: stackoverflow.com/questions/31245127/…. Seems the first syntax specifies type argument for List, whereas the second specifies type argument for contains (unnecessary in this case because the method is not generic)
    – ernest_k
    Aug 10 at 12:44






  • 3




    And, of course, BiPredicate<List<String>,String> contains1 = List<String>::<String>contains;.
    – Andy Turner
    Aug 10 at 12:45






  • 2




    As a related side-note, and a partial explanation: it's legal to supply type arguments to a non-generic method, such as list.<Number>contains("foo"). They're just ignored. (As for why the JLS authors chose to allow it, though, I don't know.)
    – Radiodef
    Aug 10 at 16:53







  • 1




    @snr Why is it legal to supply type arguments to a non-generic method? You can find the answer here.
    – Oleksandr
    Aug 10 at 20:03








7




7




Good question...
– ernest_k
Aug 10 at 12:35




Good question...
– ernest_k
Aug 10 at 12:35












Related: stackoverflow.com/questions/31245127/…. Seems the first syntax specifies type argument for List, whereas the second specifies type argument for contains (unnecessary in this case because the method is not generic)
– ernest_k
Aug 10 at 12:44




Related: stackoverflow.com/questions/31245127/…. Seems the first syntax specifies type argument for List, whereas the second specifies type argument for contains (unnecessary in this case because the method is not generic)
– ernest_k
Aug 10 at 12:44




3




3




And, of course, BiPredicate<List<String>,String> contains1 = List<String>::<String>contains;.
– Andy Turner
Aug 10 at 12:45




And, of course, BiPredicate<List<String>,String> contains1 = List<String>::<String>contains;.
– Andy Turner
Aug 10 at 12:45




2




2




As a related side-note, and a partial explanation: it's legal to supply type arguments to a non-generic method, such as list.<Number>contains("foo"). They're just ignored. (As for why the JLS authors chose to allow it, though, I don't know.)
– Radiodef
Aug 10 at 16:53





As a related side-note, and a partial explanation: it's legal to supply type arguments to a non-generic method, such as list.<Number>contains("foo"). They're just ignored. (As for why the JLS authors chose to allow it, though, I don't know.)
– Radiodef
Aug 10 at 16:53





1




1




@snr Why is it legal to supply type arguments to a non-generic method? You can find the answer here.
– Oleksandr
Aug 10 at 20:03





@snr Why is it legal to supply type arguments to a non-generic method? You can find the answer here.
– Oleksandr
Aug 10 at 20:03













3 Answers
3






active

oldest

votes

















up vote
14
down vote



accepted










First of all, that is called a type witness (in the official Oracle Tutorial) or TypeArguments (in the JLS Sec 15.12) and you are effectively helping the compiler with such constructs.



One example:



private static void test(Callable<Object> call) 



private static void test(Runnable run)



static class Gen<T>




And call it via test(Gen::new); (this will fail, never mind why), but the point is that you add a type witness to help the compiler, so this would work



test(Gen<String>::new);


So when you write List<String>, you have added a type witness for the target type - List that is; in the second case you are adding one for the method contains - but it's not generic, so it is ignored.






share|improve this answer


















  • 3




    Where does the term "type witness" come from? I can't find it in the language spec.
    – Andy Turner
    Aug 10 at 12:50






  • 3




    @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
    – Eugene
    Aug 10 at 12:51






  • 4




    @Lino the official java tutorial calls it that way, I'll stick to that :)
    – Eugene
    Aug 10 at 12:56






  • 2




    The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
    – Andy Turner
    Aug 10 at 12:58







  • 1




    @AndyTurner TypeArguments it is! thank you
    – Eugene
    Aug 10 at 13:00

















up vote
5
down vote













In:



BiPredicate<List<String>, String> contains2 = List::<String>contains;


<String> is a type argument to a non-generic List.contains method1.



While in:



BiPredicate<List<String>, String> contains1 = List<String>::contains;


<String> is a type argument to a List.




1 - In this particular case a type argument is ignored according to the JLS §15.12.2.1:




A non-generic method may be potentially applicable to an invocation
that supplies explicit type arguments. In such a case, the type
arguments will simply be ignored.







share|improve this answer






















  • Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
    – snr
    Aug 10 at 18:41






  • 1




    Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
    – Oleksandr
    Aug 10 at 19:09











  • thank you indeed
    – snr
    Aug 10 at 22:10

















up vote
4
down vote













Here's what Intellij tells me about them:



BiPredicate<List<String>, String> contains1 = List<String>::contains;



Explicit type arguments can be inferred




BiPredicate<List<String>, String> contains2 = List::<String>contains;



Type arguments are redundant for the non-generic method reference




If you were to split these up into their respective lambda functions, I believe you'd see the following:



BiPredicate<List<String>, String> contains1 = (List<String> strings, String o) -> strings.contains(o);
BiPredicate<List<String>, String> contains2 = (strings, o) -> strings.<String>contains(o);


As we know, (List<String> strings, String o) can be replaced by (strings, o) and <String> on the second line is unneeded (as String#contains isn't generic), so it's safe to assume that both method references are equivalent.






share|improve this answer




















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51786658%2fgeneric-method-reference-type-specifying-before-after-operator%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    14
    down vote



    accepted










    First of all, that is called a type witness (in the official Oracle Tutorial) or TypeArguments (in the JLS Sec 15.12) and you are effectively helping the compiler with such constructs.



    One example:



    private static void test(Callable<Object> call) 



    private static void test(Runnable run)



    static class Gen<T>




    And call it via test(Gen::new); (this will fail, never mind why), but the point is that you add a type witness to help the compiler, so this would work



    test(Gen<String>::new);


    So when you write List<String>, you have added a type witness for the target type - List that is; in the second case you are adding one for the method contains - but it's not generic, so it is ignored.






    share|improve this answer


















    • 3




      Where does the term "type witness" come from? I can't find it in the language spec.
      – Andy Turner
      Aug 10 at 12:50






    • 3




      @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
      – Eugene
      Aug 10 at 12:51






    • 4




      @Lino the official java tutorial calls it that way, I'll stick to that :)
      – Eugene
      Aug 10 at 12:56






    • 2




      The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
      – Andy Turner
      Aug 10 at 12:58







    • 1




      @AndyTurner TypeArguments it is! thank you
      – Eugene
      Aug 10 at 13:00














    up vote
    14
    down vote



    accepted










    First of all, that is called a type witness (in the official Oracle Tutorial) or TypeArguments (in the JLS Sec 15.12) and you are effectively helping the compiler with such constructs.



    One example:



    private static void test(Callable<Object> call) 



    private static void test(Runnable run)



    static class Gen<T>




    And call it via test(Gen::new); (this will fail, never mind why), but the point is that you add a type witness to help the compiler, so this would work



    test(Gen<String>::new);


    So when you write List<String>, you have added a type witness for the target type - List that is; in the second case you are adding one for the method contains - but it's not generic, so it is ignored.






    share|improve this answer


















    • 3




      Where does the term "type witness" come from? I can't find it in the language spec.
      – Andy Turner
      Aug 10 at 12:50






    • 3




      @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
      – Eugene
      Aug 10 at 12:51






    • 4




      @Lino the official java tutorial calls it that way, I'll stick to that :)
      – Eugene
      Aug 10 at 12:56






    • 2




      The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
      – Andy Turner
      Aug 10 at 12:58







    • 1




      @AndyTurner TypeArguments it is! thank you
      – Eugene
      Aug 10 at 13:00












    up vote
    14
    down vote



    accepted







    up vote
    14
    down vote



    accepted






    First of all, that is called a type witness (in the official Oracle Tutorial) or TypeArguments (in the JLS Sec 15.12) and you are effectively helping the compiler with such constructs.



    One example:



    private static void test(Callable<Object> call) 



    private static void test(Runnable run)



    static class Gen<T>




    And call it via test(Gen::new); (this will fail, never mind why), but the point is that you add a type witness to help the compiler, so this would work



    test(Gen<String>::new);


    So when you write List<String>, you have added a type witness for the target type - List that is; in the second case you are adding one for the method contains - but it's not generic, so it is ignored.






    share|improve this answer














    First of all, that is called a type witness (in the official Oracle Tutorial) or TypeArguments (in the JLS Sec 15.12) and you are effectively helping the compiler with such constructs.



    One example:



    private static void test(Callable<Object> call) 



    private static void test(Runnable run)



    static class Gen<T>




    And call it via test(Gen::new); (this will fail, never mind why), but the point is that you add a type witness to help the compiler, so this would work



    test(Gen<String>::new);


    So when you write List<String>, you have added a type witness for the target type - List that is; in the second case you are adding one for the method contains - but it's not generic, so it is ignored.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 10 at 14:44









    Andy Turner

    73.6k873121




    73.6k873121










    answered Aug 10 at 12:45









    Eugene

    57.9k978136




    57.9k978136







    • 3




      Where does the term "type witness" come from? I can't find it in the language spec.
      – Andy Turner
      Aug 10 at 12:50






    • 3




      @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
      – Eugene
      Aug 10 at 12:51






    • 4




      @Lino the official java tutorial calls it that way, I'll stick to that :)
      – Eugene
      Aug 10 at 12:56






    • 2




      The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
      – Andy Turner
      Aug 10 at 12:58







    • 1




      @AndyTurner TypeArguments it is! thank you
      – Eugene
      Aug 10 at 13:00












    • 3




      Where does the term "type witness" come from? I can't find it in the language spec.
      – Andy Turner
      Aug 10 at 12:50






    • 3




      @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
      – Eugene
      Aug 10 at 12:51






    • 4




      @Lino the official java tutorial calls it that way, I'll stick to that :)
      – Eugene
      Aug 10 at 12:56






    • 2




      The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
      – Andy Turner
      Aug 10 at 12:58







    • 1




      @AndyTurner TypeArguments it is! thank you
      – Eugene
      Aug 10 at 13:00







    3




    3




    Where does the term "type witness" come from? I can't find it in the language spec.
    – Andy Turner
    Aug 10 at 12:50




    Where does the term "type witness" come from? I can't find it in the language spec.
    – Andy Turner
    Aug 10 at 12:50




    3




    3




    @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
    – Eugene
    Aug 10 at 12:51




    @AndyTurner docs.oracle.com/javase/tutorial/java/generics/… search for "type witness", I was not saying it's in the JLS (might be, not sure)
    – Eugene
    Aug 10 at 12:51




    4




    4




    @Lino the official java tutorial calls it that way, I'll stick to that :)
    – Eugene
    Aug 10 at 12:56




    @Lino the official java tutorial calls it that way, I'll stick to that :)
    – Eugene
    Aug 10 at 12:56




    2




    2




    The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
    – Andy Turner
    Aug 10 at 12:58





    The word "witness" doesn't appear in JLS (9, at least). They are simply called TypeArguments there.
    – Andy Turner
    Aug 10 at 12:58





    1




    1




    @AndyTurner TypeArguments it is! thank you
    – Eugene
    Aug 10 at 13:00




    @AndyTurner TypeArguments it is! thank you
    – Eugene
    Aug 10 at 13:00












    up vote
    5
    down vote













    In:



    BiPredicate<List<String>, String> contains2 = List::<String>contains;


    <String> is a type argument to a non-generic List.contains method1.



    While in:



    BiPredicate<List<String>, String> contains1 = List<String>::contains;


    <String> is a type argument to a List.




    1 - In this particular case a type argument is ignored according to the JLS §15.12.2.1:




    A non-generic method may be potentially applicable to an invocation
    that supplies explicit type arguments. In such a case, the type
    arguments will simply be ignored.







    share|improve this answer






















    • Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
      – snr
      Aug 10 at 18:41






    • 1




      Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
      – Oleksandr
      Aug 10 at 19:09











    • thank you indeed
      – snr
      Aug 10 at 22:10














    up vote
    5
    down vote













    In:



    BiPredicate<List<String>, String> contains2 = List::<String>contains;


    <String> is a type argument to a non-generic List.contains method1.



    While in:



    BiPredicate<List<String>, String> contains1 = List<String>::contains;


    <String> is a type argument to a List.




    1 - In this particular case a type argument is ignored according to the JLS §15.12.2.1:




    A non-generic method may be potentially applicable to an invocation
    that supplies explicit type arguments. In such a case, the type
    arguments will simply be ignored.







    share|improve this answer






















    • Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
      – snr
      Aug 10 at 18:41






    • 1




      Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
      – Oleksandr
      Aug 10 at 19:09











    • thank you indeed
      – snr
      Aug 10 at 22:10












    up vote
    5
    down vote










    up vote
    5
    down vote









    In:



    BiPredicate<List<String>, String> contains2 = List::<String>contains;


    <String> is a type argument to a non-generic List.contains method1.



    While in:



    BiPredicate<List<String>, String> contains1 = List<String>::contains;


    <String> is a type argument to a List.




    1 - In this particular case a type argument is ignored according to the JLS §15.12.2.1:




    A non-generic method may be potentially applicable to an invocation
    that supplies explicit type arguments. In such a case, the type
    arguments will simply be ignored.







    share|improve this answer














    In:



    BiPredicate<List<String>, String> contains2 = List::<String>contains;


    <String> is a type argument to a non-generic List.contains method1.



    While in:



    BiPredicate<List<String>, String> contains1 = List<String>::contains;


    <String> is a type argument to a List.




    1 - In this particular case a type argument is ignored according to the JLS §15.12.2.1:




    A non-generic method may be potentially applicable to an invocation
    that supplies explicit type arguments. In such a case, the type
    arguments will simply be ignored.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 10 at 19:00

























    answered Aug 10 at 13:20









    Oleksandr

    6,55533263




    6,55533263











    • Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
      – snr
      Aug 10 at 18:41






    • 1




      Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
      – Oleksandr
      Aug 10 at 19:09











    • thank you indeed
      – snr
      Aug 10 at 22:10
















    • Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
      – snr
      Aug 10 at 18:41






    • 1




      Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
      – Oleksandr
      Aug 10 at 19:09











    • thank you indeed
      – snr
      Aug 10 at 22:10















    Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
    – snr
    Aug 10 at 18:41




    Is there any difference btw type parameter and type argument terms in this context? If yes, what is this?
    – snr
    Aug 10 at 18:41




    1




    1




    Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
    – Oleksandr
    Aug 10 at 19:09





    Yes, there is a difference. E in List<E> is a type parameter and the String in List<String> is a type argument. You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument - String in this case - to the List itself (source).
    – Oleksandr
    Aug 10 at 19:09













    thank you indeed
    – snr
    Aug 10 at 22:10




    thank you indeed
    – snr
    Aug 10 at 22:10










    up vote
    4
    down vote













    Here's what Intellij tells me about them:



    BiPredicate<List<String>, String> contains1 = List<String>::contains;



    Explicit type arguments can be inferred




    BiPredicate<List<String>, String> contains2 = List::<String>contains;



    Type arguments are redundant for the non-generic method reference




    If you were to split these up into their respective lambda functions, I believe you'd see the following:



    BiPredicate<List<String>, String> contains1 = (List<String> strings, String o) -> strings.contains(o);
    BiPredicate<List<String>, String> contains2 = (strings, o) -> strings.<String>contains(o);


    As we know, (List<String> strings, String o) can be replaced by (strings, o) and <String> on the second line is unneeded (as String#contains isn't generic), so it's safe to assume that both method references are equivalent.






    share|improve this answer
























      up vote
      4
      down vote













      Here's what Intellij tells me about them:



      BiPredicate<List<String>, String> contains1 = List<String>::contains;



      Explicit type arguments can be inferred




      BiPredicate<List<String>, String> contains2 = List::<String>contains;



      Type arguments are redundant for the non-generic method reference




      If you were to split these up into their respective lambda functions, I believe you'd see the following:



      BiPredicate<List<String>, String> contains1 = (List<String> strings, String o) -> strings.contains(o);
      BiPredicate<List<String>, String> contains2 = (strings, o) -> strings.<String>contains(o);


      As we know, (List<String> strings, String o) can be replaced by (strings, o) and <String> on the second line is unneeded (as String#contains isn't generic), so it's safe to assume that both method references are equivalent.






      share|improve this answer






















        up vote
        4
        down vote










        up vote
        4
        down vote









        Here's what Intellij tells me about them:



        BiPredicate<List<String>, String> contains1 = List<String>::contains;



        Explicit type arguments can be inferred




        BiPredicate<List<String>, String> contains2 = List::<String>contains;



        Type arguments are redundant for the non-generic method reference




        If you were to split these up into their respective lambda functions, I believe you'd see the following:



        BiPredicate<List<String>, String> contains1 = (List<String> strings, String o) -> strings.contains(o);
        BiPredicate<List<String>, String> contains2 = (strings, o) -> strings.<String>contains(o);


        As we know, (List<String> strings, String o) can be replaced by (strings, o) and <String> on the second line is unneeded (as String#contains isn't generic), so it's safe to assume that both method references are equivalent.






        share|improve this answer












        Here's what Intellij tells me about them:



        BiPredicate<List<String>, String> contains1 = List<String>::contains;



        Explicit type arguments can be inferred




        BiPredicate<List<String>, String> contains2 = List::<String>contains;



        Type arguments are redundant for the non-generic method reference




        If you were to split these up into their respective lambda functions, I believe you'd see the following:



        BiPredicate<List<String>, String> contains1 = (List<String> strings, String o) -> strings.contains(o);
        BiPredicate<List<String>, String> contains2 = (strings, o) -> strings.<String>contains(o);


        As we know, (List<String> strings, String o) can be replaced by (strings, o) and <String> on the second line is unneeded (as String#contains isn't generic), so it's safe to assume that both method references are equivalent.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 10 at 12:44









        Jacob G.

        12.7k31658




        12.7k31658



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51786658%2fgeneric-method-reference-type-specifying-before-after-operator%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke