1) Introducing LAMDA which are often used in Groovy, Scala(JVM used languages)
/*Arrays class contains various methods for manipulating arrays (such as sorting and searching).
Arrays class also contains a static factory that allows arrays to be viewed as lists. */
//forEach() -Performs the given action for each element of the Iterable until all elements have been
//processed or the action throws an exception.
import java.util.Arrays;
import java.util.List;
public class Lamda {
public static void main(String[] args) {
// asList converts Arrays to List... Not related to Lamda
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
System.out.println(stooges);
// by Default Compiler is in command for the list of parameters types
Arrays.asList("a", 11, "d").forEach(e -> System.out.println(e));
// Explicitly providing the type of parameter.. If not match, throws
// Exception
Arrays.asList("a", "Hello", "d").forEach((String e) -> System.out.println(e));
//Lambdas may reference the class members and local variables
String separator = ",";
Arrays.asList("a", "b", "d").forEach((String e) -> System.out.print(e + separator));
//Lamdas may return some value.
Arrays.asList( "a", "b", "d" ).sort( ( e1, e2 ) -> e1.compareTo( e2 ) );
}
}
No comments:
Post a Comment