archive-cr.com » CR » I » INFORMATECH.CR

Total: 292

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Java Lambda Expressions vs Method References | Informatech CR Blog
    4 5 List Integer numbers map Integer new digits This conveys the same meaning take a string and make me an integer out of it It is the perfect task for our Integer String constructor Instance Method Reference to Arbitrary Objects Consider now the existence of a class named Jedi defined as follows public class Jedi private String name private int power public Jedi String name int power this name name this power power public String getName return name public int getPower return power equals hashCode toString Now consider that we had a list of jedis and we would like to use our previous function map to extract the name from every jedi and generate a list of names out of the list of jedis Somewhat like this using lambda expressions List Jedi jedis asList new Jedi Obi wan 80 new Jedi Anakin 25 new Jedi Yoda 500 List String names map jedi jedi getName jedis The interesting observation here is that the parameter jedi is the argument for the apply method in the Function reference And we use that reference to a jedi to invoke on it the method getName In other words we invoke a method on the reference we receive as argument So we could simplify this implementation by using an instance method reference as follows List Jedi jedi asList new Jedi Obi wan 80 new Jedi Anakin 25 new Jedi Yoda 500 List String names map Jedi getName jedi Again the interesting aspect of this type of method reference is that the method getName is an instance method Therefore the target of its invocation must be an instance which in this case is an arbitrary object being provided as the argument for the method apply in the Function interface definition Instance Method Reference to a Specific Object Let s consider the existence of functional interface named Consumer as follows public interface Consumer T public void accept T t And let s define a method capable of using a consumer to consume all the elements of a given list like this static void forEach Consumer T consumer List T source for T item source consumer accept item Imagine that now we would like to print all the elements contained in a list and for that purpose we could define a consumer using a lambda expressions List Integer numbers asList 1 2 3 4 5 6 7 8 9 forEach n System out println n numbers However we could also make the observation that the method println has the same signature that our Consumer has it receives an integer and does something with it in this case it prints it to the main output However we cannot specify that this is an arbitrary instance method reference by saying PrintStream println because in this case the Consumer interface method accept does not receive as one of its arguments the PrintStream object on which we may want to invoke the method println Conversely we already know which is the target

    Original URL path: http://blog.informatech.cr/2013/03/15/java-lambda-expressions-vs-method-references/?replytocom=2418 (2015-08-19)
    Open archived version from archive


  • Java Lambda Expressions vs Method References | Informatech CR Blog
    2 3 4 5 List Integer numbers map Integer new digits This conveys the same meaning take a string and make me an integer out of it It is the perfect task for our Integer String constructor Instance Method Reference to Arbitrary Objects Consider now the existence of a class named Jedi defined as follows public class Jedi private String name private int power public Jedi String name int power this name name this power power public String getName return name public int getPower return power equals hashCode toString Now consider that we had a list of jedis and we would like to use our previous function map to extract the name from every jedi and generate a list of names out of the list of jedis Somewhat like this using lambda expressions List Jedi jedis asList new Jedi Obi wan 80 new Jedi Anakin 25 new Jedi Yoda 500 List String names map jedi jedi getName jedis The interesting observation here is that the parameter jedi is the argument for the apply method in the Function reference And we use that reference to a jedi to invoke on it the method getName In other words we invoke a method on the reference we receive as argument So we could simplify this implementation by using an instance method reference as follows List Jedi jedi asList new Jedi Obi wan 80 new Jedi Anakin 25 new Jedi Yoda 500 List String names map Jedi getName jedi Again the interesting aspect of this type of method reference is that the method getName is an instance method Therefore the target of its invocation must be an instance which in this case is an arbitrary object being provided as the argument for the method apply in the Function interface definition Instance Method Reference to a Specific Object Let s consider the existence of functional interface named Consumer as follows public interface Consumer T public void accept T t And let s define a method capable of using a consumer to consume all the elements of a given list like this static void forEach Consumer T consumer List T source for T item source consumer accept item Imagine that now we would like to print all the elements contained in a list and for that purpose we could define a consumer using a lambda expressions List Integer numbers asList 1 2 3 4 5 6 7 8 9 forEach n System out println n numbers However we could also make the observation that the method println has the same signature that our Consumer has it receives an integer and does something with it in this case it prints it to the main output However we cannot specify that this is an arbitrary instance method reference by saying PrintStream println because in this case the Consumer interface method accept does not receive as one of its arguments the PrintStream object on which we may want to invoke the method println Conversely we already know which is

    Original URL path: http://blog.informatech.cr/2013/03/15/java-lambda-expressions-vs-method-references/?replytocom=2429 (2015-08-19)
    Open archived version from archive

  • Java Lambda Expressions vs Method References | Informatech CR Blog
    5 List Integer numbers map Integer new digits This conveys the same meaning take a string and make me an integer out of it It is the perfect task for our Integer String constructor Instance Method Reference to Arbitrary Objects Consider now the existence of a class named Jedi defined as follows public class Jedi private String name private int power public Jedi String name int power this name name this power power public String getName return name public int getPower return power equals hashCode toString Now consider that we had a list of jedis and we would like to use our previous function map to extract the name from every jedi and generate a list of names out of the list of jedis Somewhat like this using lambda expressions List Jedi jedis asList new Jedi Obi wan 80 new Jedi Anakin 25 new Jedi Yoda 500 List String names map jedi jedi getName jedis The interesting observation here is that the parameter jedi is the argument for the apply method in the Function reference And we use that reference to a jedi to invoke on it the method getName In other words we invoke a method on the reference we receive as argument So we could simplify this implementation by using an instance method reference as follows List Jedi jedi asList new Jedi Obi wan 80 new Jedi Anakin 25 new Jedi Yoda 500 List String names map Jedi getName jedi Again the interesting aspect of this type of method reference is that the method getName is an instance method Therefore the target of its invocation must be an instance which in this case is an arbitrary object being provided as the argument for the method apply in the Function interface definition Instance Method Reference to a Specific Object Let s consider the existence of functional interface named Consumer as follows public interface Consumer T public void accept T t And let s define a method capable of using a consumer to consume all the elements of a given list like this static void forEach Consumer T consumer List T source for T item source consumer accept item Imagine that now we would like to print all the elements contained in a list and for that purpose we could define a consumer using a lambda expressions List Integer numbers asList 1 2 3 4 5 6 7 8 9 forEach n System out println n numbers However we could also make the observation that the method println has the same signature that our Consumer has it receives an integer and does something with it in this case it prints it to the main output However we cannot specify that this is an arbitrary instance method reference by saying PrintStream println because in this case the Consumer interface method accept does not receive as one of its arguments the PrintStream object on which we may want to invoke the method println Conversely we already know which is the target object

    Original URL path: http://blog.informatech.cr/2013/03/15/java-lambda-expressions-vs-method-references/?replytocom=2451 (2015-08-19)
    Open archived version from archive

  • Java Lambda Expressions Basics | Informatech CR Blog
    within the code block or void if nothing is returned What is a Java Functional Interface In Java a functional interface is basically an interface with a single abstract method This kind of interfaces are also known as SAM Single Abstract Method types Before Java 8 there were already several interfaces compatible with this idea public interface Comparator T int compare T o1 T o2 public interface Callable V V call throws Exception public interface ActionListener extends EventListener public void actionPerformed ActionEvent e public interface Runnable public void run Java 8 Examples Many new functional interfaces are being defined in the Java 8 among the most popular those found in the new java util function package The following are some examples of new functional interfaces in Java public interface Predicate T boolean test T t public interface Function T R R apply T t public interface BinaryOperator T T apply T left T right public interface Consumer T void accept T t public interface Supplier T T get Starting with Java 8 these functional interfaces can be implemented by means of lambda expressions and method references What is the Type of Lambda Expression In languages that support first class functions the type of the lambda expression would be a function but in Java the lambda expressions are represented as objects and so they must be bound to a particular object type known as a functional interface This is called the target type Since a functional interface can only have a single abstract method the types of the lambda expression parameters must correspond to the parameters in that method and the type of the lambda body must correspond to the return type of this method Additionally any exceptions thrown in the lambda body must be allowed by the throws clause of this method in the functional interface The following are examples of lambda expressions bound to a target type Predicate Integer isOdd n n 2 0 BinaryOperator Integer sum x y x y Callable Integer callMe 42 Block String printer String s System out println s Runnable runner System out println Hello World See the Java documentation for the new java util function package to get more details on these functional interfaces and their corresponding abstract methods Notice that the type of the lambda expression is determined by the compiler from the context based on the target type This implies that two apparently equal lambda expressions may have different types simply because they are bound to a different target type as demonstrated in the following code Callable String callMe Hello PrivilegedAction String action Hello Example Supposing we had a functional interface named Predicate declared as follows public interface Predicate T boolean test T input Now imagine that we had a method capable of filtering the contents of a Collection based on a provided Predicate as follows public static T Collection T filter Predicate T predicate Collection T items Collection T result new ArrayList T for T item items if predicate test

    Original URL path: http://blog.informatech.cr/2013/03/22/java-lambda-expressions-basics/?shared=email&msg=fail (2015-08-19)
    Open archived version from archive