DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java Streams API Preview

Java Streams API Preview

Edwin Dalorzo user avatar by
Edwin Dalorzo
·
Apr. 11, 13 · Interview
Like (0)
Save
Tweet
Share
9.28K Views

Join the DZone community and get the full member experience.

Join For Free

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:

lambda-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

  • java streams preview vs. .net high-order programming with linq
  • java infinite streams

futher reading

  • jdk 8 early access
  • project lambda
  • jep 107: bulk data operations for collections
  • where is the java collections framework going?
Stream (computing) API Java (programming language) Sales

Published at DZone with permission of Edwin Dalorzo. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Use MQTT in Java
  • 5 Factors When Selecting a Database
  • Debugging Threads and Asynchronous Code
  • Using the PostgreSQL Pager With MariaDB Xpand

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: