Last active
August 29, 2015 14:15
-
-
Save uttesh/0a7e0f43f68ac04d7b51 to your computer and use it in GitHub Desktop.
This file contains 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 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 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment