Over a million developers have joined DZone.

Java Streams API Preview

DZone 's Guide to

Java Streams API Preview

· Java Zone ·
Free Resource

For 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)
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"))

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))
if(byYoungest.isPresent()) {

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)
mostCostlySale = sales.sorted( byCost )
if(mostCostlySale.isPresent()) {

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())

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)
if(ageOfYoungest.isPresent()) {

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 )

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) )
.sorted( comparing(toSize).reverseOrder() )
if(mostBought.isPresent()) {

Related Posts

Futher Reading


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}