• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which collection to use?

 
Tommy Mato
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map (HashMap or otherwise) is definitely the way to go.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • 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
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be able to help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic