aspose file tools*
The moose likes Beginning Java and the fly likes Which collection to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which collection to use?" Watch "Which collection to use?" New topic
Author

Which collection to use?

Tommy Mato
Greenhorn

Joined: Dec 14, 2007
Posts: 26

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

A Map (HashMap or otherwise) is definitely the way to go.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
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

Joined: Dec 14, 2007
Posts: 26

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

Joined: Oct 13, 2005
Posts: 38334
    
  23
Glad to be able to help
 
Consider Paul's rocket mass heater.
 
subject: Which collection to use?