Author Topic: Java Maps, Pass-by-Value and Saving  (Read 1445 times)

0 Members and 1 Guest are viewing this topic.

Offline Flipside

  • əp!sd!l£
  • 212
Java Maps, Pass-by-Value and Saving
Well, not really the place to ask, but I thought I'd start here, since it's not as if SCP can't lay claim to some pretty competent programmers of its own :)

I'm toying around with something in Java, and trying to get my head around something. Say I've created a Class called TradeItem, which is stored as a Vector in Java. Now, say that I have also created a HashMap, which references TradeItems and stores a list of other objects, e.g:

Code: [Select]
ArrayList<TradeItem> allItems = null;
HashMap<TradeItem, ArrayList<Market>> m = new HashMap<TradeItem, ArrayList<Market>>();

So, obviously, this Map allows the system to look up which markets sell which items. Now, supposing I were to save these to disc using the ObjectStream function as two seperate
files, reset the JVM and load them back into the system the same way. What does my memory now contain? From what I recall, HashMap uses the HashCode of the key to generate a position to store the data, but that would, as far as I can tell, rely on the data always being in the same place in memory, which may not be the case after a reset? And if that is not the case, does the system keep two copies of the TradeItem list on reload? Which is something I want to avoid.

I'm assuming the system will cope as long as the List is loaded first, which is automatically cross referenced somehow with the Map once it is loaded, but if someone can understand this and point me in the right direction, I'd appreciate it, I think I've thought about it too hard the last few hours...


Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: Java Maps, Pass-by-Value and Saving
You could always override the getHashCode function, if you need to.


Offline Flipside

  • əp!sd!l£
  • 212
Re: Java Maps, Pass-by-Value and Saving
True, but if I were going to do that, it might be cleaner just to keep a list of TradeItems and a seperate list of Markets, load in both lists, and then generate the reference Map from those lists at boot-up. The idea is that TradeItem would just be a template, there'd only be one instance of the TradeItem class for each item, regardless of how many copies are made, so 400 pairs of shoes would, in truth, be a much smaller class with a reference to the TradeItem class for shoes in it and an integer of how many copies, so each instance of the template could theoretically search through the markets at boot and find the ones that sell it, and generate its own internal list without filling too much memory. That way, you could be certain that one set of data was directly referencing the other, but I'm not sure how costly that would get in CPU time.

That, after a nights sleep, seems to be the best way to approach it if I want to make certain that all cross-referencing is under control...
« Last Edit: April 09, 2010, 10:44:26 am by Flipside »