Simplified List operations using Lambdas
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class Lambdas_ListOperations {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Gallen","Burchhill"),
new Person("Allen","churchhill"),
new Person("Alex","garten"),
new Person("Sam","Batley"));
//Sorting List the usual way
// Collections.sort(people,new Comparator<Person>(){
// @Override
// public int compare(Person person1, Person person2) {
// return person1.firstName.compareTo(person2.firstName);
// };
// });
//(OR)
//Sorting List the Lambda way
Collections.sort(people,
(person1,person2)-> person1.firstName.compareTo(person2.firstName));
//PRINT all using regular method
// for(Person person : people){
// System.out.println(person.firstName);
// }
//(OR)
//PRINT all the Lambda way
printAllFromListUsingLambdas(people,
(person1)->System.out.println(person1.firstName));
//PRINT all users with firstNames starting with "A"
// printConditionally(people);
//(OR)
//PRINT all users with firstNames starting with "A" the Lambda way
printConditionallyUsingLambdas(people,
person1->person1.firstName.startsWith("A"),
person1->System.out.println(person1.firstName));
}
private static void printConditionallyUsingLambdas(List<Person> people, Predicate<Person> predicate, Consumer<Person> consumer) {
for(Person person1 : people){
if(predicate.test(person1)) consumer.accept(person1);
}
}
private static void printAllFromListUsingLambdas(List<Person> people, Consumer<Person> consumer) {
for(Person person : people)
{
consumer.accept(person);
}
}
private static void printConditionally(List<Person> people) {
for(Person person : people){
if(person.firstName.startsWith("A")) System.out.println(person.firstName);
}
}
}
class Person{
String firstName;
String lastName;
public Person(String firstName,String lastName) {
this.firstName = firstName;
this.lastName=lastName;
}
@Override
public String toString() {
return this.firstName+" "+this.lastName;
}
}
Note:
Important out-of-the-box APIs provided in java 8(java.util.Function.Predicate)
- Consumer<Object>..Its a functional method with a single method called accept(Object). This method takes in an object but returns nothing.. instead it s accept method can be implemented in printing or performing some logic as in the above example.
- Predicate<Object>..Its a functional Interface with a single method called test(Object) and returns a boolean. This method takes in an object and returns boolean.. Its test method can be implemented in checking a certain condition as in the example above
- BiCosumer<Object1, Object2>..This Functional interface is very similar to Consumer interface.. the only difference is that it takes 2 objects and returns nothing as in Consumer.
- There are many more OOTB interfaces provided in java 8 for Lambda usage.
- @FunctionaInterface is the annotation that can you used above an interface that is intended for Lambda functions. This way, the developers can be informed of the interfaces that are used in code by Lambda declarations; which in turn will disable them from altering it.
________________________________________________________________________
No comments:
Post a Comment