From cc0329efbb5fb9330e6c79a40f8dd2967bda5931 Mon Sep 17 00:00:00 2001 From: "Shawn M. Hanes" Date: Wed, 11 Apr 2018 18:12:59 -0400 Subject: [PATCH] [Java/en] Added Lambdas section. --- java.html.markdown | 164 ++++++++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/java.html.markdown b/java.html.markdown index 18a3b21a..ca0b04c2 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -11,7 +11,7 @@ contributors: - ["Michael Dähnert", "https://github.com/JaXt0r"] - ["Rob Rose", "https://github.com/RobRoseKnows"] - ["Sean Nam", "https://github.com/seannam"] - - ["Shawn M. Hanes", "https://github.com/smhanes15"] + - ["Shawn M. Hanes", "https://github.com/smhanes15"] filename: LearnJava.java --- @@ -879,87 +879,87 @@ import java.util.function.*; import java.security.SecureRandom; public class Lambdas { - public static void main(String[] args) { - // Lambda declaration syntax: - // -> - - // We will use this hashmap in our examples below. - Map planets = new HashMap<>(); - planets.put("Mercury", "87.969"); - planets.put("Venus", "224.7"); - planets.put("Earth", "365.2564"); - planets.put("Mars", "687"); - planets.put("Jupiter", "4,332.59"); - planets.put("Saturn", "10,759"); - planets.put("Uranus", "30,688.5"); - planets.put("Neptune", "60,182"); - - // Lambda with zero parameters using the Supplier functional interface - // from java.util.function.Supplier. The actual lambda expression is - // what comes after numPlanets =. - Supplier numPlanets = () -> Integer.toString(planets.size()); - System.out.format("Number of Planets: %s\n\n", numPlanets.get()); - - // Lambda with one parameter and using the Consumer functional interface - // from java.util.function.Consumer. This is because planets is a Map, - // which implements both Collection and Iterable. The forEach used here, - // found in Iterable, applies the lambda expression to each member of - // the Collection. The default implementation of forEach behaves as if: - /* - for (T t : this) - action.accept(t); - */ - - // The actual lambda expression is the parameter passed to forEach. - planets.keySet().forEach((p) -> System.out.format("%s\n", p)); - - // If you are only passing a single argument, then the above can also be - // written as (note absent parentheses around p): - planets.keySet().forEach(p -> System.out.format("%s\n", p)); - - // Tracing the above, we see that planets is a HashMap, keySet() returns - // a Set of its keys, forEach applies each element as the lambda - // expression of: (parameter p) -> System.out.format("%s\n", p). Each - // time, the element is said to be "consumed" and the statement(s) - // referred to in the lambda body is applied. Remember the lambda body - // is what comes after the ->. - - // The above without use of lambdas would look more traditionally like: - for (String planet : planets.keySet()) { - System.out.format("%s\n", planet); - } - - // This example differs from the above in that a different forEach - // implementation is used: the forEach found in the HashMap class - // implementing the Map interface. This forEach accepts a BiConsumer, - // which generically speaking is a fancy way of saying it handles - // the Set of each Key -> Value pairs. This default implementation - // behaves as if: - /* - for (Map.Entry entry : map.entrySet()) - action.accept(entry.getKey(), entry.getValue()); - */ - - // The actual lambda expression is the parameter passed to forEach. - String orbits = "%s orbits the Sun in %s Earth days.\n"; - planets.forEach((K, V) -> System.out.format(orbits, K, V)); - - // The above without use of lambdas would look more traditionally like: - for (String planet : planets.keySet()) { - System.out.format(orbits, planet, planets.get(planet)); - } - - // Or, if following more closely the specification provided by the - // default implementation: - for (Map.Entry planet : planets.entrySet()) { - System.out.format(orbits, planet.getKey(), planet.getValue()); - } - - // These examples cover only the very basic use of lambdas. It might not - // seem like much or even very useful, but remember that a lambda can be - // created as an object that can later be passed as parameters to other - // methods. - } + public static void main(String[] args) { + // Lambda declaration syntax: + // -> + + // We will use this hashmap in our examples below. + Map planets = new HashMap<>(); + planets.put("Mercury", "87.969"); + planets.put("Venus", "224.7"); + planets.put("Earth", "365.2564"); + planets.put("Mars", "687"); + planets.put("Jupiter", "4,332.59"); + planets.put("Saturn", "10,759"); + planets.put("Uranus", "30,688.5"); + planets.put("Neptune", "60,182"); + + // Lambda with zero parameters using the Supplier functional interface + // from java.util.function.Supplier. The actual lambda expression is + // what comes after numPlanets =. + Supplier numPlanets = () -> Integer.toString(planets.size()); + System.out.format("Number of Planets: %s\n\n", numPlanets.get()); + + // Lambda with one parameter and using the Consumer functional interface + // from java.util.function.Consumer. This is because planets is a Map, + // which implements both Collection and Iterable. The forEach used here, + // found in Iterable, applies the lambda expression to each member of + // the Collection. The default implementation of forEach behaves as if: + /* + for (T t : this) + action.accept(t); + */ + + // The actual lambda expression is the parameter passed to forEach. + planets.keySet().forEach((p) -> System.out.format("%s\n", p)); + + // If you are only passing a single argument, then the above can also be + // written as (note absent parentheses around p): + planets.keySet().forEach(p -> System.out.format("%s\n", p)); + + // Tracing the above, we see that planets is a HashMap, keySet() returns + // a Set of its keys, forEach applies each element as the lambda + // expression of: (parameter p) -> System.out.format("%s\n", p). Each + // time, the element is said to be "consumed" and the statement(s) + // referred to in the lambda body is applied. Remember the lambda body + // is what comes after the ->. + + // The above without use of lambdas would look more traditionally like: + for (String planet : planets.keySet()) { + System.out.format("%s\n", planet); + } + + // This example differs from the above in that a different forEach + // implementation is used: the forEach found in the HashMap class + // implementing the Map interface. This forEach accepts a BiConsumer, + // which generically speaking is a fancy way of saying it handles + // the Set of each Key -> Value pairs. This default implementation + // behaves as if: + /* + for (Map.Entry entry : map.entrySet()) + action.accept(entry.getKey(), entry.getValue()); + */ + + // The actual lambda expression is the parameter passed to forEach. + String orbits = "%s orbits the Sun in %s Earth days.\n"; + planets.forEach((K, V) -> System.out.format(orbits, K, V)); + + // The above without use of lambdas would look more traditionally like: + for (String planet : planets.keySet()) { + System.out.format(orbits, planet, planets.get(planet)); + } + + // Or, if following more closely the specification provided by the + // default implementation: + for (Map.Entry planet : planets.entrySet()) { + System.out.format(orbits, planet.getKey(), planet.getValue()); + } + + // These examples cover only the very basic use of lambdas. It might not + // seem like much or even very useful, but remember that a lambda can be + // created as an object that can later be passed as parameters to other + // methods. + } } ```