This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

ArrayList Sum Value by Key

 
Ranch Hand
Posts: 57
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there,
I need your help, cause I don't know how to solve it.
This list is in a arraylist<runners> runners and the entity is the same as the topic.

runner, style, date, distance
Micky Maus, free-style, 2019-06-05, 4654
Goofy, speedrun, 2019-01-02, 5798
Donald Duck, cross, 2019-08-15, 4581
Micky Maus, cross, 2019-04-05, 8709
Donald Duck, normal, 2019-08-15, 4711
Micky Maus, walking, 2019-07-06, 4541

What I need the total sum of the distance for each runner (style and date can be ignore).

My first idea was to create a map like this one:



My expectation is a list with:
Micky Maus, 17904
Donald Duck, 9292
Goofy, 5798

for any help I'am appreciative, Thanks
Cheers Chris
 
Bartender
Posts: 3590
151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Chris,

indeed you can create a Map<String, Integer> as follows:
 
Marshal
Posts: 66135
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good solution, Piet, but please explain some more in case the code is unfamiliar to CE.
 
Piet Souris
Bartender
Posts: 3590
151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here goes...

Since java 8 it is quite easy to create a Map from a List of Objects, here: Runners.
For this, we firste create a Stream of all the individual Runner instances, by issuing: list.stream
With 'collect(' we specify what to do with these elements, and the following 'groupingBy' indicates that we wmat to make a Map. Now, what java needs to know is on what basis must the grouping being based, and next, what we need to report of each grouping element.
For the grouping variable, we must specify a key, and we do that by saying how to derive that key from a Runner. So we state:

so it is the name of the Runners that we will do the grouping.
Next is to make clear what we want to report for all these names. We do that with this (looks a bit complex, and takes quite some getting used to):

so, for each Runner we want to know about that Runners distances.
From here, there are quite some possibilities. For instance:

and a very interesting one:

Indeed, not so easy stuff, but very convenient once you get the hang of it.

Here is a more traditional solution:


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

Piet Souris wrote:. . .. . .

Could you use runner::name or runner::getName instead? Would have have to write an additional method to get those method references to work?You would need additional methods, wouldn't you.
 
Chris Ernst
Ranch Hand
Posts: 57
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much and for the explanation!
Did you write it in Java 10 or higher? Maybe then I have to upgrade in the future, I'am still using Java 8 and the script is not working

And yes Campbell was right, its my first contact with Map... I prefer ArrayList don't know why but it is so ;)


It confused me (its long ago that I code in Java ;), your explanation was great! )  I decided to use the traditinal way

So thank you both very much for your, fast, help!

Cheers
 
Campbell Ritchie
Marshal
Posts: 66135
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris Ernst wrote:. . . thank you both . . .

That's a pleasure

What went wrong when you used Java8? I would have thought that code would work in Java8 but not Java7. Maybe I was mistaken.
 
Chris Ernst
Ranch Hand
Posts: 57
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 8 didn't know to handle var it would create a new class. Or NetBeans 8.2 can't handel it ;)

But with the traditional way from Piet works it as I want
 
Campbell Ritchie
Marshal
Posts: 66135
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The identifier var wasn't introduced until Java10, but maybe Piet has already told you that.
 
brevity is the soul of wit - shakepeare. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!