• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

ArrayList Table Sum

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the best way to Sum Arraylist table different values?

For example we have a table:

Name                Time(s)                 State                        
John                      54                    Florida
Mike                      48                    Texas
Kyrie                     32                    Texas
Joe                        21                    Florida
Josh                      45                    Cali
Robert                   60                    Texas

Let's say we have the table info stored in an Arraylist
What algorithm would you use to calculate the time totals of states?

The final result should look like this:

State                       Time
Florida                      75
Texas                       140
Cali                           45
 
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is the data stored in the ArrayList?  Is there a class that holds the data that is shown on each row of the posted table?
Does the class have get methods for all the data items held in the class?
 
Pvo Gri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Data is stored from a json file. Yes, there is a class that holds the data and it also has getters and setters.
 
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Pvo Gri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am familiar with it, but I wouldn't say I fully know how to
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at using a Map<String, TheCounter> where the key is the name of the state, and the value is an instance of TheCounter which is your class that holds the sum of the times.
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TheCounter could be as simple as Integer.
 
Bartender
Posts: 2845
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pvo Gri wrote:What would be the best way to Sum Arraylist table different values?


The oracle tutorial mentions this: https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
You can find the example for "Map Interface Basic Operations" that calculates frequency. It perfectly fits your requirement if you know the tinkering required
 
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Needless to say there is a way to create a Map with a Stream. But you need to read the file into a List<Player> first. I shall leave it as an exercise for the reader and might let you out of your misery in due course.
 
Campbell Ritchie
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you have a List<Player> called players, you can do this:-Has somebody given me a cow? Thank you whoever it was
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still trying to learn streams so I copied the code into my test environment and compiled it, getting these errors:
 
Campbell Ritchie
Marshal
Posts: 72406
315
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because I have “corrected” the code by moving some () and got them in the wrong places. Sorry. The summingInt() call creates a Collector and that is a second argument to groupingBy().
 
Saloon Keeper
Posts: 4346
163
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A useful alternative is the method 'map.merge'.
For instance:

And using static imports is a very nice typesaver.
 
please buy my thing and then I'll have more money:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic