1

Here is my problem:

Consider that I have a big list of baseball information. Each entry in this list is a homerun a player has hit, recorded by his name and the team he was on when he hit the homerun. It would look something like:

Player    |      Team
tim             eagles
john            bears
frank           lions
tim             lions
frank           bears
john            lions
john            bears
tim             eagles
tim             eagles
frank           eagles

I am trying to understand how I would store this data in a form where each player has a list of Key:Value pairs, where K=(team name), V=(# of homeruns hit while playing for that team). This set of key:values should be ordered by # of homeruns.

This is just the way I am thinking of solving the problem, but I really have no idea if this is the best way to do it. The end goal is to be able to spit out another list of the form:

Player   |    Team    |    Homeruns
 tim         eagles           3
 john        bears            2
 frank       lions            1
 frank       eagles           1
 john        lions            1
 tim         lions            1
 frank       bears            1

How might I go about creating such a data structure?

My main idea so far has: ArrayList of TreeMaps where each TreeMap corresponds to a Player.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ac927
  • 35
  • 5

4 Answers4

2

Usually it's not a good idea to have nested collections. Instead, use collections of custom objects that have another collection.

In your case, you should have a Player class that has a Set or List of home runs, which would also be a custom class.

class Player{
    List<HomeRun> homeRuns;
}

class HomeRun{
    String team;
}

Now you could make Player implement Comparable<Player> and order them by the size of the homeRuns collection, then you can store your players in a TreeSet.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
1

How about...

class Player{
  Map<String, Integer> homeRuns; 
}

And then elsewhere, you could have a List of type Player.

But I also agree with what PNS said and I'd read up on data structures and POJOs.

Chris
  • 839
  • 2
  • 10
  • 33
  • this is the direction i think i should go. It will be best to create classes for the various parts of my program, rather than trying to make one big confusing set of linked collections – ac927 Aug 04 '16 at 18:11
0

That's how programming is. Many different solutions so it depends on what you need as you say. If you know that you're not going to need more than the information you present in your question your solution with list and map should be fine. However, perhaps consider what to do if you would at some point needed to get the number of homeruns each team had ever scored or had to add in which season the homerun was made.

I propose to look into database design which is perhaps not what you need for this but has a lot of knowledge on making extensible design.

PNS
  • 750
  • 1
  • 5
  • 19
0

A good DS for this can be:

HashMap<Player, HashMap<Team, Integer>>

For each player you can get in O(1) time, another HashMap whose keyset can be iterated over to find team this player played for and the home-runs hit for that team.

If you want print the home-runs per player in a sorted order, there are other variations that can be used. Also, O(1) may be an overkill for you. Maybe O(n) would be good for you. You will have to be more specific about your use case for the required data structure.

displayName
  • 13,888
  • 8
  • 60
  • 75
  • thank you, but i think it will be best to create classes for the parts of my program – ac927 Aug 04 '16 at 18:10
  • @ac927: Of course. `Player` and `Team` in my answer are classes only that you will have to build [according to your application's needs](http://stackoverflow.com/a/34071142/1835769). – displayName Aug 04 '16 at 18:17