Lambdaj
Lambdaj
Lambdaj
group
sort
extract
lambdaj version:
String brands = joinFrom(db.getCars()).getBrand();
lambdaj version:
List<Sale> salesOfAFerrari = select(sales, having(on(Sale.class).getCar().getBrand(),equalTo("Ferrari")));
lambdaj version:
List<Sale> sales = select(sales,having(on(Sale.class).getBuyer(), equalTo(selectMin(persons, on(Person.class).getAge()))));
lambdaj version:
Sol. 1 -> double maxCost = max(sales, on(Sale.class).getCost()); Sol. 2 -> double maxCost = maxFrom(sales).getCost();
lambdaj version:
double sum = sumFrom(select(sales, having(on(Sale.class).getBuyer().isMale()).and( having(on(Sale.class).getSeller().isMale())))).getCost();
lambdaj version:
int age = min(forEach(select(sales, having(on(Sale.class).getCost(), greaterThan(50000.00)))) .getBuyer(), on(Person.class).getAge());
lambdaj version:
List<Sale> sortedSales = sort(sales, on(Sale.class).getCost());
lambdaj version:
List<Double> costs = extract(cars, on(Car.class).getOriginalValue());
lambdaj version:
Map<String, Car> carsByBrand = index(cars, on(Car.class).getBrand());
Performance analysis
Minimum, maximum and average duration in milliseconds of 10 runs of 100,000 iterations of the former examples
PrintAllBrands FindAllSalesOfAFerrari FindAllBuysOfYoungestPerson FindMostCostlySaleValue SumCostsWhereBothActorsAreAMale AgeOfYoungestBuyerForMoreThan50K SortSalesByCost ExtractCarsOriginalCost IndexCarsByBrand GroupSalesByBuyersAndSellers FindMostBoughtCar min 656 688 13.485 531 843 12.890 3.250 328 469 23.657 8.296 iterative max 1.016 766 14.969 547 875 13.063 3.500 344 500 24.079 8.484 avg 769 761 14.757 544 859 12.965 3.421 333 485 23.835 8.395 min 5.812 6.172 18.953 3.546 10.828 24.860 21.953 1.218 1.484 36.375 28.421 lambdaj max 6.266 6.297 19.375 3.579 11.141 25.234 22.312 1.250 1.531 37.046 28.859 avg 5.905 6.214 19.124 3.564 11.021 25.089 22.080 1.228 1.511 36.686 28.614 ratio 7,679 8,166 1,296 6,551 12,830 1,935 6,454 3,688 3,115 1,539 3,408
Average ratio =
5,151
Known limitations
lambdaj cannot Lack of reified generics infer the actual type to be returned when a null or empty collection is passed to forEach()
List<Person> persons = new ArrayList<Person>(); forEach(persons).setLastName("Fusco"); Exception
Impossibility to proxy a final class the on() construct cannot register an invocation after a final Class is met
List<Person> sortedByNamePersons = sort(persons, on(Person.class).getName().toLowerCase()); Exception
code.google.com/p/lambdaj
Thank you
Mario Fusco
mario@exmachina.ch