This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Which collection to use?

 
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a program that needs to keep scores for each player in a game. I want to record these in a collection, but I'm not sure which collection would be best.

The number of players can vary and new players can join the game at any time. I want to keep a cumulative total for each player e.g.

If the scores occur in this order:
Tom 3
Susan 4
Joe 5
Susan 6
Tom 1


At the end of the game, I would like a list of the players and their total scores: e.g.
Tom = 4
Susan = 10
Joe = 5

I know I could build a HashMap and manage the accumulations myself, but I can't help worry that I might be missing a trick. If you know something better than HashMap for this problem then I'd be very grateful for your help.

Tom
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map (HashMap or otherwise) is definitely the way to go.
 
Marshal
Posts: 65100
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. Use a Map<Player, Score> where Score is a class you write yourself which counts scores to make a cumulative total.
 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt responses. I had an idea I was on the right lines but was not sure. I'm half-way to getting it working now. Thanks again!
 
Campbell Ritchie
Marshal
Posts: 65100
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be able to help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!