Java Streams API Preview
Join the DZone community and get the full member experience.
Join For Freefor today’s post i want to share a series of examples that i have developed while trying the latest features in the java 8 stream api. in my last post i did a comparison of the features with those available in linq, and for today’s post i have decided to go a bit further and try to use the api to work with a small domain model. the examples developed here are based on the same examples presented in the lambdaj project .
the data model
for the examples i will use the following domain model:
you can see a full implementation of all the examples developed here by downloading the following gist .
for the examples presented below assume that in the context of the code there are three streams always available:
-
stream<person> persons
. -
stream<car> cars
. -
stream<sale> sales
.
challenge 1: print all car brands
from a collection of cars print all car brands.
stringjoiner brands = cars.map(car::getbrand) .collect(tostringjoiner(",")); string allbrands = brands.tostring();
for this example i have also use the new
stringjoiner
class.
challenge 2: select all sales on toyota
from a collection of sales, select all those that are related to toyota cars.
list<sale> toyotasales; toyotasales = sales.filter(s -> s.getcar().getbrand().equals("toyota")) .collect(tolist()); toyotasales.foreach(system.out::println);
for this example i could have also used the
foreach
method in the stream to get all the sales printed. i did it this way just to illustrate that it is possible to collect all items in the stream into a list and from there we can process them. but ideally, i should have processed the items directly in the stream.
challenge 3: find buys of the youngest person
from a collection of sales, find all those that are from the youngest buyer.
tointfunction<entry<person, list<sale>>> byage; byage = e -> e.getkey().getage(); byyoungest = sales.collect(groupingby(sale::getbuyer)) .entryset() .stream() .sorted(comparing(byage)) .map(entry::getvalue) .findfirst(); if(byyoungest.ispresent()) { system.out.println(byyoungest.get()); }
challenge 4: find most costly sale
from a collection of sales, find the most costly of all of them.
optional<sale> mostcostlysale; comparator<sale> bycost = comparing((todoublefunction<sale>)sale::getcost) .reverseorder(); mostcostlysale = sales.sorted( bycost ) .findfirst(); if(mostcostlysale.ispresent()) { system.out.println(mostcostlysale.get()); }
challenge 5: sum of sales from male buyers & sellers
from a collection of sales find the sum of all buys/sells made by men.
doublesum = sales.filter(s -> s.getbuyer().ismale() && s.getseller().ismale()) .maptodouble(sale::getcost) .sum();
challenge 6: find the age of the youngest buyer
from a collection of sales, find the age of the youngest buyer who bought for more than 40,000.
optionalint ageofyoungest; ageofyoungest = sales.filter(sale -> sale.getcost() > 40000) .map(sale::getbuyer) .maptoint(person::getage) .sorted() .findfirst(); if(ageofyoungest.ispresent()) { system.out.println(ageofyoungest.getasint()); }
challenge 7: sort sales by cost
sort a collection of sales by cost.
comparator<sale> bycost= comparing((todoublefunction<sale>) sale::getcost); list<sale> sortedbycost; sortedbycost = sales.sorted( bycost ) .collect(tolist());
challenge 8: index cars by brand
from a collection of cars, index cars by the their brand.
map<string,list<car>> bybrand; bybrand = cars.collect( groupingby(car::getbrand ));
challenge 9: find most bought car
from a collection of sales find the most bought car.
tointfunction<entry<car,list<sale>>> tosize = (e -> e.getvalue().size()); optional<car> mostbought; mostbought = sales.collect( groupingby(sale::getcar) ) .entryset() .stream() .sorted( comparing(tosize).reverseorder() ) .map(entry::getkey) .findfirst(); if(mostbought.ispresent()) { system.out.println(mostbought.get()); }
related posts
futher reading
Published at DZone with permission of Edwin Dalorzo. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments