How to bind a Java Supplier to an instance of an object?

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











up vote
8
down vote

favorite
1












How can I bind a Java Supplier to an existing instance of an Object? For example, if I want to write my own compareTo() method with this header:



public static int myCompareTo(Object o1, Object o2, Supplier<Comparable> supplier) ...


I want be able to call it like:



myCompareTo("Hello", "Hello2", String::length);


where String (with the capital letter) is a class and no object. So how can I bind the instance o1 to the supplier?










share|improve this question









New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • btw you could accept an answer here....
    – Eugene
    15 hours ago














up vote
8
down vote

favorite
1












How can I bind a Java Supplier to an existing instance of an Object? For example, if I want to write my own compareTo() method with this header:



public static int myCompareTo(Object o1, Object o2, Supplier<Comparable> supplier) ...


I want be able to call it like:



myCompareTo("Hello", "Hello2", String::length);


where String (with the capital letter) is a class and no object. So how can I bind the instance o1 to the supplier?










share|improve this question









New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • btw you could accept an answer here....
    – Eugene
    15 hours ago












up vote
8
down vote

favorite
1









up vote
8
down vote

favorite
1






1





How can I bind a Java Supplier to an existing instance of an Object? For example, if I want to write my own compareTo() method with this header:



public static int myCompareTo(Object o1, Object o2, Supplier<Comparable> supplier) ...


I want be able to call it like:



myCompareTo("Hello", "Hello2", String::length);


where String (with the capital letter) is a class and no object. So how can I bind the instance o1 to the supplier?










share|improve this question









New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











How can I bind a Java Supplier to an existing instance of an Object? For example, if I want to write my own compareTo() method with this header:



public static int myCompareTo(Object o1, Object o2, Supplier<Comparable> supplier) ...


I want be able to call it like:



myCompareTo("Hello", "Hello2", String::length);


where String (with the capital letter) is a class and no object. So how can I bind the instance o1 to the supplier?







java method-reference






share|improve this question









New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









LAD

1,637719




1,637719






New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Philipp Niedergesäß

648




648




New contributor




Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • btw you could accept an answer here....
    – Eugene
    15 hours ago
















  • btw you could accept an answer here....
    – Eugene
    15 hours ago















btw you could accept an answer here....
– Eugene
15 hours ago




btw you could accept an answer here....
– Eugene
15 hours ago












4 Answers
4






active

oldest

votes

















up vote
3
down vote













Here's what you were searching for (I believe):



public static <T, U extends Comparable<U>> int compare(T o1, T o2, Function<T, U> mapper) 
return mapper.apply(o1).compareTo(mapper.apply(o2));



You can call that like so:



compare("str1", "str2", String::length); // 0





share|improve this answer




















  • I think you were the closest, here, but not entirely close
    – Eugene
    2 days ago

















up vote
3
down vote













Actually a more correct way to define your method would be:



private static <T, U extends Comparable<? super U>> int myCompareTo(T left, T right, Function<T, U> fu) 
return Comparator.comparing(fu).compare(left, right);






share|improve this answer



























    up vote
    1
    down vote













    You can use



    Comparator.comparing(String::length);


    to obtain a comparator instance which you can pass to the method.






    share|improve this answer


















    • 1




      Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
      – Oleksandr
      2 days ago











    • Of course it will. I tried to satisfy the provided myCompareTo interface.
      – senjin.hajrulahovic
      2 days ago






    • 1




      @Oleksandr should have made that an answer IMO, if you didn't I did
      – Eugene
      2 days ago

















    up vote
    1
    down vote



    accepted










    Thanks for your answers. Actually I figured it out now. I wanted to have the supplied object instances (o1 and o2) to execute the given method. I found out that Supplier was the wrong interface instead I had to use Function. Here you can see my working simplified example:



    public static <T> int myCompareTo(T o1, T o2, Function<T, Comparable> getter) 
    return getter.apply(o1).compareTo(getter.apply(o2));



    The reason, the interface has to be Function and not Supplier is, that only Function is equivalent to a lambda expression taking an object and calls the referenced method on the object.



    For example, if you define the method reference as:



    Function<TypeOfInstance, ReturnTypeOfReferencedMethod> methodReference = TypeOfInstance::referencedMethod();


    then the equivalent lambda expression being executed is:



    (instance) -> instance.referencedMethod()


    Additional Information:



    Edit: I know I could have done the same by using Comparator, but this example is very simplified. In my application a Function of this kind is neccessary. I had to create a compareTo function that sorts an ArrayList by more than one attribute because the main sorting attribute may not be unique in the list. I want to share my code with you, because I think it can be a interesting insight for you.



    public static <T> int ultimateCompare(T o1, T o2, Function<T, Comparable>... getters) 
    for (Function<T, Comparable> getter : getters)
    int result = getter.apply(o1).compareTo(getter.apply(o2));
    if (result != 0) return result;

    return 0;



    With this for example, you can sort a list of persons by last name and if two of them are identical, you can use the first name to sort. With this solution you can change sorting at runtime.






    share|improve this answer










    New contributor




    Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
      – László van den Hoek
      yesterday










    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
    );



    );






    Philipp Niedergesäß is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52682507%2fhow-to-bind-a-java-supplier-to-an-instance-of-an-object%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    Here's what you were searching for (I believe):



    public static <T, U extends Comparable<U>> int compare(T o1, T o2, Function<T, U> mapper) 
    return mapper.apply(o1).compareTo(mapper.apply(o2));



    You can call that like so:



    compare("str1", "str2", String::length); // 0





    share|improve this answer




















    • I think you were the closest, here, but not entirely close
      – Eugene
      2 days ago














    up vote
    3
    down vote













    Here's what you were searching for (I believe):



    public static <T, U extends Comparable<U>> int compare(T o1, T o2, Function<T, U> mapper) 
    return mapper.apply(o1).compareTo(mapper.apply(o2));



    You can call that like so:



    compare("str1", "str2", String::length); // 0





    share|improve this answer




















    • I think you were the closest, here, but not entirely close
      – Eugene
      2 days ago












    up vote
    3
    down vote










    up vote
    3
    down vote









    Here's what you were searching for (I believe):



    public static <T, U extends Comparable<U>> int compare(T o1, T o2, Function<T, U> mapper) 
    return mapper.apply(o1).compareTo(mapper.apply(o2));



    You can call that like so:



    compare("str1", "str2", String::length); // 0





    share|improve this answer












    Here's what you were searching for (I believe):



    public static <T, U extends Comparable<U>> int compare(T o1, T o2, Function<T, U> mapper) 
    return mapper.apply(o1).compareTo(mapper.apply(o2));



    You can call that like so:



    compare("str1", "str2", String::length); // 0






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    steffen

    8,35412052




    8,35412052











    • I think you were the closest, here, but not entirely close
      – Eugene
      2 days ago
















    • I think you were the closest, here, but not entirely close
      – Eugene
      2 days ago















    I think you were the closest, here, but not entirely close
    – Eugene
    2 days ago




    I think you were the closest, here, but not entirely close
    – Eugene
    2 days ago












    up vote
    3
    down vote













    Actually a more correct way to define your method would be:



    private static <T, U extends Comparable<? super U>> int myCompareTo(T left, T right, Function<T, U> fu) 
    return Comparator.comparing(fu).compare(left, right);






    share|improve this answer
























      up vote
      3
      down vote













      Actually a more correct way to define your method would be:



      private static <T, U extends Comparable<? super U>> int myCompareTo(T left, T right, Function<T, U> fu) 
      return Comparator.comparing(fu).compare(left, right);






      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        Actually a more correct way to define your method would be:



        private static <T, U extends Comparable<? super U>> int myCompareTo(T left, T right, Function<T, U> fu) 
        return Comparator.comparing(fu).compare(left, right);






        share|improve this answer












        Actually a more correct way to define your method would be:



        private static <T, U extends Comparable<? super U>> int myCompareTo(T left, T right, Function<T, U> fu) 
        return Comparator.comparing(fu).compare(left, right);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Eugene

        62.7k986145




        62.7k986145




















            up vote
            1
            down vote













            You can use



            Comparator.comparing(String::length);


            to obtain a comparator instance which you can pass to the method.






            share|improve this answer


















            • 1




              Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
              – Oleksandr
              2 days ago











            • Of course it will. I tried to satisfy the provided myCompareTo interface.
              – senjin.hajrulahovic
              2 days ago






            • 1




              @Oleksandr should have made that an answer IMO, if you didn't I did
              – Eugene
              2 days ago














            up vote
            1
            down vote













            You can use



            Comparator.comparing(String::length);


            to obtain a comparator instance which you can pass to the method.






            share|improve this answer


















            • 1




              Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
              – Oleksandr
              2 days ago











            • Of course it will. I tried to satisfy the provided myCompareTo interface.
              – senjin.hajrulahovic
              2 days ago






            • 1




              @Oleksandr should have made that an answer IMO, if you didn't I did
              – Eugene
              2 days ago












            up vote
            1
            down vote










            up vote
            1
            down vote









            You can use



            Comparator.comparing(String::length);


            to obtain a comparator instance which you can pass to the method.






            share|improve this answer














            You can use



            Comparator.comparing(String::length);


            to obtain a comparator instance which you can pass to the method.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            senjin.hajrulahovic

            446413




            446413







            • 1




              Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
              – Oleksandr
              2 days ago











            • Of course it will. I tried to satisfy the provided myCompareTo interface.
              – senjin.hajrulahovic
              2 days ago






            • 1




              @Oleksandr should have made that an answer IMO, if you didn't I did
              – Eugene
              2 days ago












            • 1




              Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
              – Oleksandr
              2 days ago











            • Of course it will. I tried to satisfy the provided myCompareTo interface.
              – senjin.hajrulahovic
              2 days ago






            • 1




              @Oleksandr should have made that an answer IMO, if you didn't I did
              – Eugene
              2 days ago







            1




            1




            Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
            – Oleksandr
            2 days ago





            Comparator.comparing(String::length).compare("Hello1", "Hello2"); will be shorter :)
            – Oleksandr
            2 days ago













            Of course it will. I tried to satisfy the provided myCompareTo interface.
            – senjin.hajrulahovic
            2 days ago




            Of course it will. I tried to satisfy the provided myCompareTo interface.
            – senjin.hajrulahovic
            2 days ago




            1




            1




            @Oleksandr should have made that an answer IMO, if you didn't I did
            – Eugene
            2 days ago




            @Oleksandr should have made that an answer IMO, if you didn't I did
            – Eugene
            2 days ago










            up vote
            1
            down vote



            accepted










            Thanks for your answers. Actually I figured it out now. I wanted to have the supplied object instances (o1 and o2) to execute the given method. I found out that Supplier was the wrong interface instead I had to use Function. Here you can see my working simplified example:



            public static <T> int myCompareTo(T o1, T o2, Function<T, Comparable> getter) 
            return getter.apply(o1).compareTo(getter.apply(o2));



            The reason, the interface has to be Function and not Supplier is, that only Function is equivalent to a lambda expression taking an object and calls the referenced method on the object.



            For example, if you define the method reference as:



            Function<TypeOfInstance, ReturnTypeOfReferencedMethod> methodReference = TypeOfInstance::referencedMethod();


            then the equivalent lambda expression being executed is:



            (instance) -> instance.referencedMethod()


            Additional Information:



            Edit: I know I could have done the same by using Comparator, but this example is very simplified. In my application a Function of this kind is neccessary. I had to create a compareTo function that sorts an ArrayList by more than one attribute because the main sorting attribute may not be unique in the list. I want to share my code with you, because I think it can be a interesting insight for you.



            public static <T> int ultimateCompare(T o1, T o2, Function<T, Comparable>... getters) 
            for (Function<T, Comparable> getter : getters)
            int result = getter.apply(o1).compareTo(getter.apply(o2));
            if (result != 0) return result;

            return 0;



            With this for example, you can sort a list of persons by last name and if two of them are identical, you can use the first name to sort. With this solution you can change sorting at runtime.






            share|improve this answer










            New contributor




            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
              – László van den Hoek
              yesterday














            up vote
            1
            down vote



            accepted










            Thanks for your answers. Actually I figured it out now. I wanted to have the supplied object instances (o1 and o2) to execute the given method. I found out that Supplier was the wrong interface instead I had to use Function. Here you can see my working simplified example:



            public static <T> int myCompareTo(T o1, T o2, Function<T, Comparable> getter) 
            return getter.apply(o1).compareTo(getter.apply(o2));



            The reason, the interface has to be Function and not Supplier is, that only Function is equivalent to a lambda expression taking an object and calls the referenced method on the object.



            For example, if you define the method reference as:



            Function<TypeOfInstance, ReturnTypeOfReferencedMethod> methodReference = TypeOfInstance::referencedMethod();


            then the equivalent lambda expression being executed is:



            (instance) -> instance.referencedMethod()


            Additional Information:



            Edit: I know I could have done the same by using Comparator, but this example is very simplified. In my application a Function of this kind is neccessary. I had to create a compareTo function that sorts an ArrayList by more than one attribute because the main sorting attribute may not be unique in the list. I want to share my code with you, because I think it can be a interesting insight for you.



            public static <T> int ultimateCompare(T o1, T o2, Function<T, Comparable>... getters) 
            for (Function<T, Comparable> getter : getters)
            int result = getter.apply(o1).compareTo(getter.apply(o2));
            if (result != 0) return result;

            return 0;



            With this for example, you can sort a list of persons by last name and if two of them are identical, you can use the first name to sort. With this solution you can change sorting at runtime.






            share|improve this answer










            New contributor




            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
              – László van den Hoek
              yesterday












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            Thanks for your answers. Actually I figured it out now. I wanted to have the supplied object instances (o1 and o2) to execute the given method. I found out that Supplier was the wrong interface instead I had to use Function. Here you can see my working simplified example:



            public static <T> int myCompareTo(T o1, T o2, Function<T, Comparable> getter) 
            return getter.apply(o1).compareTo(getter.apply(o2));



            The reason, the interface has to be Function and not Supplier is, that only Function is equivalent to a lambda expression taking an object and calls the referenced method on the object.



            For example, if you define the method reference as:



            Function<TypeOfInstance, ReturnTypeOfReferencedMethod> methodReference = TypeOfInstance::referencedMethod();


            then the equivalent lambda expression being executed is:



            (instance) -> instance.referencedMethod()


            Additional Information:



            Edit: I know I could have done the same by using Comparator, but this example is very simplified. In my application a Function of this kind is neccessary. I had to create a compareTo function that sorts an ArrayList by more than one attribute because the main sorting attribute may not be unique in the list. I want to share my code with you, because I think it can be a interesting insight for you.



            public static <T> int ultimateCompare(T o1, T o2, Function<T, Comparable>... getters) 
            for (Function<T, Comparable> getter : getters)
            int result = getter.apply(o1).compareTo(getter.apply(o2));
            if (result != 0) return result;

            return 0;



            With this for example, you can sort a list of persons by last name and if two of them are identical, you can use the first name to sort. With this solution you can change sorting at runtime.






            share|improve this answer










            New contributor




            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            Thanks for your answers. Actually I figured it out now. I wanted to have the supplied object instances (o1 and o2) to execute the given method. I found out that Supplier was the wrong interface instead I had to use Function. Here you can see my working simplified example:



            public static <T> int myCompareTo(T o1, T o2, Function<T, Comparable> getter) 
            return getter.apply(o1).compareTo(getter.apply(o2));



            The reason, the interface has to be Function and not Supplier is, that only Function is equivalent to a lambda expression taking an object and calls the referenced method on the object.



            For example, if you define the method reference as:



            Function<TypeOfInstance, ReturnTypeOfReferencedMethod> methodReference = TypeOfInstance::referencedMethod();


            then the equivalent lambda expression being executed is:



            (instance) -> instance.referencedMethod()


            Additional Information:



            Edit: I know I could have done the same by using Comparator, but this example is very simplified. In my application a Function of this kind is neccessary. I had to create a compareTo function that sorts an ArrayList by more than one attribute because the main sorting attribute may not be unique in the list. I want to share my code with you, because I think it can be a interesting insight for you.



            public static <T> int ultimateCompare(T o1, T o2, Function<T, Comparable>... getters) 
            for (Function<T, Comparable> getter : getters)
            int result = getter.apply(o1).compareTo(getter.apply(o2));
            if (result != 0) return result;

            return 0;



            With this for example, you can sort a list of persons by last name and if two of them are identical, you can use the first name to sort. With this solution you can change sorting at runtime.







            share|improve this answer










            New contributor




            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited 2 days ago





















            New contributor




            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 2 days ago









            Philipp Niedergesäß

            648




            648




            New contributor




            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Philipp Niedergesäß is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.











            • You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
              – László van den Hoek
              yesterday
















            • You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
              – László van den Hoek
              yesterday















            You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
            – László van den Hoek
            yesterday




            You can use Comparator.thenComparing to construct a Comparator that will take into account an arbitrary number of attributes, in order.
            – László van den Hoek
            yesterday










            Philipp Niedergesäß is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Philipp Niedergesäß is a new contributor. Be nice, and check out our Code of Conduct.












            Philipp Niedergesäß is a new contributor. Be nice, and check out our Code of Conduct.











            Philipp Niedergesäß is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52682507%2fhow-to-bind-a-java-supplier-to-an-instance-of-an-object%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?