• Post Reply Bookmark Topic Watch Topic
  • New Topic

Maps, Lists, Arrays: I could use a pragmatic opinion  RSS feed

 
Adam Nadeau
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no shortage of collection classes in Java, and that has left me spending more time deciding how to approach my project than actually coding it. The bottom line is, I'm trying to throw together a fantasy football draft application (for my use, not for distribution) and I can't decide on the best way of handling the data portion of the program. I need to read in 700-800 Player objects from a file, which are essentially random little groups of data (firstName, lastName, position, team); there are no unique identifiers in this situation. Then, I have to remove them one by one as they are drafted, which changes all of the original index numbers. I had actually written code to read the file into an ArrayList, but that becomes cumbersome as the players are removed from the list (drafted) as the index numbers constantly change. My current thought is to read the file into a regular array and set up a separate ArrayList<Integer> to use as an iterating tool; basically, assign each Player Object an integer ID number as it is created and control access to the array through loops over the separate ArrayList. I'm thinking that this way, each ID number will always correspond to the proper array index, and the ID ArrayList will only refer to available players. Anyhow, I could really use some informed opinions of the best/easiest way to go about this. This little project (which has to be done in four weeks, before the start of football season) is my personal foray into learning Java. With that in mind, I'm not asking anyone to write code for me (I'll learn faster by trying and failing), but I am asking that you don't give me any overly technical answers (I only recognize a few built-in classes and methods at this point. Thank you in advance to anyone who can give me some advice.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Sounds to me like a Map with your generated ids as keys.

P.S. I suggest using paragraphs in posts. Big blocks of text are hard to read.
 
Adam Nadeau
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Welcome to the Ranch.

Sounds to me like a Map with your generated ids as keys.

P.S. I suggest using paragraphs in posts. Big blocks of text are hard to read.


I had expected my question to be much shorter, but I lost track as I added information. Thanks for the welcome, though!

As for the Map, I was looking into that (specifically TreeMap as HashMap "doesn't guarantee" sort order), but each ID is going to be effectively random. The original file is contrived from publicly available data and sorted in an unpredictable way, but IDs will be assigned in the order in which they appear in the file. In a search or filter capacity, is it possible to traverse the map without an ID? For example, is it possible to return every Map entry that matches Player.lastName or Player.position? Then, if I can display Players like that, would it then be possible to extract and pass the ID to remove a Player from the Map?

I realize some of these questions seem sophomoric, but I have yet to see an explanation of the Map class that didn't include reinventing the wheel; literally, I haven't seen an example that didn't use "extends" and a half dozen custom methods just to bend the Map class to the coder's will.

By the way, thanks for answering so quickly.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map is definitely the correct approach. As for what kind of Map, TreepMap (a sorted by key map) or HashMap or some other map.

Depending on how you want to work with the data, the map key can be the player's name (first + last or last + first). The value can be that player's info ( eg Player class ) but since you are working with hundreds of objects, memory may become an issue.

As each player is drafted, removing that player (name) from the map is straightforward calling map.remove(key)

If you want to store your players by say position, a Map<Position, List<Player>> can do the trick. The Position can be a string.

Given this removing a player from the list again is just calling remove(player).
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Have you read the Java™ Tutorials about collections? Look at the sections about set interface, map interface, and list interface. Imagine you have a lot of telephone numbers. If you put them in a set, there will be one entry for each phone number and duplicates vanish and if you try to iterate the set they don't come out in any obvious order.
Enter 123 234 345 345 345 456
Contains 123 234 345 456
If you use a List and enter the phone numbers and iterate it, you get the first entered first and the last entered last, and if you enter the same number twice, the duplicates will appear.
Enter 123 234 345 234 345 456 567 234
Contains 123 234 345 234 345 456 567 234
If you use a Map, you have to enter two pieces of data, e.g. name and phone number. You would then find your map contains things like
Campbell↦8****2
Campbell's wife↦8****2
Campbell's daughter↦07***8
etc.

Does that help with the difference between the three? If you go through the tutorial link, you find there are special classes which allow you to get the contents of a set in a particular order, and all sorts of other things. If you use a Map or certain kinds of Set, you need to beware of putting mutable types in them; it is possible that they can change their state and vanish from sight. In the case of the Map, the name should be immutable. If the phone number object changes its state, that is all right.
 
Adam Nadeau
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that answers my question; I will look into recoding as a Map. Thanks everyone.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One extra thing to add: I'm not sure what tutorials you read but it is almost certainly a bad idea to be extending any of the collection implementations. If there are tutorials teaching you to do that then they probably aren't good tutorials.
 
Adam Nadeau
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't actually read anything that explicitly suggested that, it was an idea that occurred to me in the absence of a defined list of JButton customization methods. The Java tutorials from Oracle (and most others I've seen) have been virtually useless in the area between beginner and career programmer. They show you how to make one and give a couple examples that demonstrate different use cases. Outside of that, there is just the page that defines the object and gives a short list of defined methods and dozens of links for inheritance; I don't have time to fish through that much information hoping to stumble upon the line that fits my goals. However, I did find yesterday some examples of ways to manipulate JButtons, so I will go that route instead of trying to create a host of event handlers for JLabels.
 
Adam Nadeau
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute, I just realized you were referring to my reply, not my original post. That didn't come from a tutorial, that was gleaned from reading through posts at other forums. I know that Java has enough built-in functionality to accomplish most tasks, but it seems too many people like to use stock classes as suggestions rather than their intended purposes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!