Lambda expressions are a new and important feature included in Java SE 8. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. In addition, new concurrency features improve performance.
Lambda Expression Syntax
(Arguments) (Arrow Token) (Body) (int x,int y) -> x+y
Arguments:
argument list is like normal method level parameters list, it can be no argument also. i.e.
() -> 121
it will return 121 value and no argument list.
Body:
body can be statement block or logical block with return statement.
more samples :
(String s) -> { System.out.println(s); }
block form just print the string.
a lambda could be represented as a comma-separated list of parameters, the –> symbol and the body. For example:
Arrays.asList( "Item1", "Item2", "Item3" ).forEach( e -> System.out.println( e ) );
Please notice the type of argument e is being inferred by the compiler. Alternatively, you may explicitly provide the type of the parameter.
Arrays.asList("Item1", "Item2", "Item3").forEach((String e) -> System.out.println(e));
If expression/processing in complex then add the brackets i.e.
Arrays.asList("Item1", "Item2", "Item3").forEach(e -> { System.out.print(e); }); String separator = ","; Arrays.asList( "Item1", "Item2", "Item3" ).forEach( { ( String e ) -> System.out.print( e + separator ) });
lambda expression will return the value as well. i.e.
Arrays.asList( "Item1", "Item2", "Item3" ).sort( ( e1, e2 ) -> e1.compareTo( e2 ) ); Arrays.asList( "Item1", "Item2", "Item3" ).sort( ( e1, e2 ) -> { int result = e1.compareTo( e2 ); return result; } );
Functional Interfaces:
a Functional Interface is an interface with just one abstract method declared in it.java.lang.Runnable is an example of a Functional Interface. There is only one method void run() declared in Runnable interface. We use Anonymous inner classes to instantiate objects of functional interface. With Lambda expressions.
// Anonymous Runnable Runnable r = new Runnable(){ @Override public void run(){ System.out.println("Hello world old!"); } }; // Lambda Runnable Runnable r = () -> System.out.println("Hello world new!");
Comparator Lambda expression
traditional compator sort </hr> Collections.sort(list, new Comparator<Person>() { public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } }); Lambda sort </hr> Collections.sort(list, (Person p1, Person p2) -> p1.getName().compareTo(p2.getName()));
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
/** | |
* | |
* @author uttesh.blogspot.com | |
*/ | |
public class HelloLambda { | |
public static void main(String[] args) { | |
Arrays.asList("a", "b", "d").forEach(e -> System.out.println(e)); | |
Arrays.asList("a", "b", "d").forEach((String e) -> System.out.println(e)); | |
Arrays.asList("a", "b", "d").forEach(e -> { | |
System.out.print(e); | |
}); | |
String separator = ","; | |
Arrays.asList("a", "b", "d").forEach( | |
(String e) -> System.out.print(e + separator)); | |
Arrays.asList("a", "b", "d").sort((e1, e2) -> { | |
int result = e1.compareTo(e2); | |
return result; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.uttesh.lambda.model.Person; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
/** | |
* | |
* @author uttesh.blogspot.com | |
*/ | |
public class LambdaComparatorSample { | |
public static void main(String[] args) { | |
List<Person> list = Person.populate(); | |
//traditional comparator sort | |
traditionComparatorSort(list); | |
// Use Lambda instead | |
lambdaSort(list); | |
} | |
public static void traditionComparatorSort(List<Person> list) { | |
// Sort with Inner Class | |
Collections.sort(list, new Comparator<Person>() { | |
public int compare(Person p1, Person p2) { | |
return p1.getName().compareTo(p2.getName()); | |
} | |
}); | |
System.out.println("=== Sorted Asc Name by traditional comparator ==="); | |
for (Person p : list) { | |
System.out.println(" person name :" + p.getName()); | |
} | |
} | |
public static void lambdaSort(List<Person> list) { | |
Collections.sort(list, (Person p1, Person p2) -> p1.getName().compareTo(p2.getName())); | |
System.out.println("=== Sorted Asc Name by lambdaSort ==="); | |
for (Person p : list) { | |
System.out.println(" person name :" + p.getName()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.chrono.IsoChronology; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* | |
* @author uttesh.blogspot.com | |
*/ | |
public class Person { | |
public enum Sex { | |
MALE, FEMALE | |
} | |
String name; | |
LocalDate birthday; | |
Sex gender; | |
String emailAddress; | |
Person(String nameArg, LocalDate birthdayArg, | |
Sex genderArg, String emailArg) { | |
name = nameArg; | |
birthday = birthdayArg; | |
gender = genderArg; | |
emailAddress = emailArg; | |
} | |
public int getAge() { | |
return birthday | |
.until(IsoChronology.INSTANCE.dateNow()) | |
.getYears(); | |
} | |
public void printPerson() { | |
System.out.println(name + ", " + this.getAge()); | |
} | |
public Sex getGender() { | |
return gender; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getEmailAddress() { | |
return emailAddress; | |
} | |
public LocalDate getBirthday() { | |
return birthday; | |
} | |
public static int compareByAge(Person a, Person b) { | |
return a.birthday.compareTo(b.birthday); | |
} | |
public static List<Person> populate() { | |
List<Person> roster = new ArrayList<>(); | |
roster.add( | |
new Person( | |
"Uttesh", | |
IsoChronology.INSTANCE.date(1984, 12, 20), | |
Person.Sex.MALE, | |
"uttesh.kumar@gmail.com")); | |
roster.add( | |
new Person( | |
"Sonali", | |
IsoChronology.INSTANCE.date(1990, 7, 15), | |
Person.Sex.FEMALE, "sonali@example.com")); | |
roster.add( | |
new Person( | |
"Raghu", | |
IsoChronology.INSTANCE.date(1991, 8, 13), | |
Person.Sex.MALE, "raghu@example.com")); | |
roster.add( | |
new Person( | |
"Yallanki", | |
IsoChronology.INSTANCE.date(2000, 9, 12), | |
Person.Sex.MALE, "Yallanki@example.com")); | |
return roster; | |
} | |
} |
references
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
0 comments:
Post a Comment