Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Count total price of items in HashMap via Streams

 
Ranch Hand
Posts: 35
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to remind myself streams and started small project with shopping cart. However I don't know how to get total price of items, which I want to count via Stream API.
How to get into collection, use getPrice() method (method of ShoppingItem, which is the key in HashMap), multiply it by quantity (Value of Hashmap) and get sum of this multiplications?
Here is  shrunk down code:




 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't iterate a Map. only one of the three sets you can get out of it. You can get access to those three sets via three of the Map's methods. You can use the map() mathod to take the price and count and produce a number. If you have the price as a primitive you may be able to get a Stream of primitives with a method called mapToXYZ() and use its sum() method, otherwise you would have to write a Collector with its three parts yourself, or use the reduce() method which you will find examples of in the package documentation.
Show us what you have tried and tell us how it is going. This is one of the circumstances where it is much easier to use an IDE than the command line.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not related to your question, but I believe that the method putItem() in ShoppingCart can be simplified to one line:
 
Andrzej Zahorski
Ranch Hand
Posts: 35
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can't iterate a Map. only one of the three sets you can get out of it. You can get access to those three sets via three of the Map's methods. You can use the map() mathod to take the price and count and produce a number. If you have the price as a primitive you may be able to get a Stream of primitives with a method called mapToXYZ() and use its sum() method, otherwise you would have to write a Collector with its three parts yourself, or use the reduce() method which you will find examples of in the package documentation.
Show us what you have tried and tell us how it is going. This is one of the circumstances where it is much easier to use an IDE than the command line.




I was thinking about something like this, however I couldn't put it in code

 
Andrzej Zahorski
Ranch Hand
Posts: 35
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I wrote something like this:


It seems to work.
 
Bartender
Posts: 4006
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it works, well done!

Instead of using .map and .reduce you can use .mapToInt and .sum, as Campbell hinted.
But you can even go one line shorter:

 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . well done! . . .

Agree with Piet, that's good But Piet has made some good suggestions for improvements.
 
Why is the word "abbreviation" so long? And this ad is so short?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic