Skip to content

Instantly share code, notes, and snippets.

@hackmajoris
Last active September 22, 2016 13:42
Show Gist options
  • Save hackmajoris/ec54797c86f88db158d50049fb89a5d1 to your computer and use it in GitHub Desktop.
Save hackmajoris/ec54797c86f88db158d50049fb89a5d1 to your computer and use it in GitHub Desktop.
More complex lambda examples
import java.util.*;
import java.util.function.Predicate;
/**
* Created by iliesa on 22.09.2016.
*/
public class App {
private ArrayList<User> users;
public App() {
users = new ArrayList<User>();
setMockData();
// listAllPersonsUsingLambda();
// printUsersWithAnInstatiatedPredicate();
// printUsersUsingLambdasDirectlyForFilteringTheSpanishOnes();
// printUsersUsingLambdasDirectlyForFilteringTheSpanishOnesThatHaveAgeAboveFive();
// printOlderUser();
}
private void setMockData() {
ArrayList<String> contryArray = new ArrayList(Arrays.asList(Country.ITALY, Country.SPAIN, Country.IRELAND));
for (int i = 0; i < 20; i++) {
User user = new User("user" + i, i, contryArray.get(new Random().nextInt(3)));
users.add(user);
}
}
List<User> filterUsersByAge(List<User> users, UserPredicat p) {
List<User> result = new ArrayList<>();
for (User user : users) {
if (p.testAge(user.getAge())) {
result.add(user);
}
}
return result;
}
private void listAllPersonsUsingLambda() {
this.users.forEach(person -> {
System.out.println(person.getCountry());
});
}
private void printUsersWithAnInstatiatedPredicate() {
Predicate italy = p -> {
return Country.ITALY.equals(((User) p).getCountry());
};
this.users.stream().filter(italy).forEach((person) -> {
System.out.println(((User) person).getCountry());
});
}
private void printUsersUsingLambdasDirectlyForFilteringTheSpanishOnes() {
this.users.stream().filter(person -> {
return Country.SPAIN.equals(person.getCountry());
}).forEach((eachPerson) -> {
System.out.println(eachPerson.getCountry());
});
}
private void printUsersUsingLambdasDirectlyForFilteringTheSpanishOnesThatHaveAgeAboveFive() {
this.users.stream().filter(person -> {
return Country.SPAIN.equals(person.getCountry()) && person.getAge() > 5;
}).forEach((eachPerson) -> {
System.out.println("Age: " + eachPerson.getAge() + "Country: " + eachPerson.getCountry());
});
}
private void printUsersCountUsingLambdasDirectlyForFilteringTheSpanishOnesThatHaveAgeAboveFive() {
this.users.stream().filter(person -> {
return Country.SPAIN.equals(person.getCountry()) && person.getAge() > 5;
}).count();
}
public void printOlderUser() {
Predicate italy = p -> {
return Country.ITALY.equals(((User) p).getCountry());
};
this.users.stream().filter(italy).forEach((person) -> {
System.out.print(((User) person).getCountry());
System.out.println(((User) person).getAge());
});
//not older then 5
User us = this.users.stream().filter(person -> {
return Country.ITALY.equals(person.getCountry()) && person.getAge() < 5;
}).max(Comparator.comparing(User::getAge)).get();
System.out.println(us.getAge());
}
public static void main(String[] args) {
new App();
}
}
/**
* Created by iliesa on 22.09.2016.
*/
public interface Country {
static String SPAIN = "Spain";
static String ITALY = "Italy";
static String IRELAND = "Ireland";
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.BiFunction;
/**
* Created by iliesa on 22.09.2016.
*/
public class SimpleLamdaExamples {
public static void main(String[] args) {
new SimpleLamdaExamples().example3();
}
public void example1() {
BiFunction addition = (x, y) -> (int) x + (int) y;
System.out.println("result: " + addition.apply(1, 5));
}
public void example2() {
Arrays.asList("e1", "e1", "e3", "e4").forEach(e -> System.out.println(e));
}
public void example3()
{
ArrayList<String> arrayList = new ArrayList((Arrays.asList("e1", "e1", "e3f", "e3")));
Collections.sort(arrayList,(String a, String b) -> b.compareTo(a) );
arrayList.forEach(e-> System.out.println(e));
}
}
/**
* Created by iliesa on 22.09.2016.
*/
public class User {
private String usename;
private int age;
private String country;
public User(String usename, int age, String country) {
this.usename = usename;
this.age = age;
this.country = country;
}
public String getUsername() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment