0

I decided to do server-side sorting. I have a HashMap with Objects (key is a String, value is an ItemDTO). These are loaded statically upon server start.

For every character (CharacterDTO) that has an amount of items (in this case ItemOnCharacterDTO) I need to be able to sort stuff. So my client sends a request for sorting the objects as well as some filter-data. What I now want to do is:

  1. get the full ItemDTOs based on the IDs saved in ItemOnCharacterDTOs
  2. add those ItemDTOs into a list
  3. sort the ItemDTOs inside that list (probably using a Comparator)
  4. create an Array of IDs (it corresponds to the sequence of the list of ItemDTOs after the sorting)
  5. discard the list of ItemDTOs that I've previously created, since I got the correct order after sorting already

My question is now: do I need to copy the ItemDTOs before adding them to the list in order to not interfere with several sortings going on at the same time?

I do change the order of ItemDTOs within the newly created list of ItemDTOs (step 2 above), but I don't change the ItemDTOs themselves (hence they are static and within the HashMap I mentioned at the beginning).

Thank you for your help in advance!

3
  • If the itemDTOs are immutable, there is no need to copy them. If not it depends. Can you share some code? Commented Dec 3, 2011 at 20:05
  • I think you're over thinking the issue. An ArrayList<T> is of T references to an object. Are you operating on these references? If you are then you probably want a copy. If you're just looking for the order you're fine. Also--if you're going to the server (and from the server) you're probably serializing the objects which amounts to a deep clone every time they're transmitted. The point might be moot. Commented Dec 3, 2011 at 20:07
  • Yes, ItemDTO and ItemOnCharacterDTO both are serializable. I'm also sure that I won't be changing the ItemDTOs. However: I will probably use some kind of Comparator (or compare-interface) in order to be able to create the order specified by the filtering settings (e.g. sort by name, type, price, ownerID). I'm not sure, but I think brain answered my question: moving around the references within an array does not affect the underlying objects - that's probably what I really wanted to know. I'm marking that response as correct for now. Huge thanks everyone! Commented Dec 3, 2011 at 21:18

1 Answer 1

2

You only need to copy the individual objects if they are mutable i.e. if they might be changed in a way that would effect the sort order or filtering. In Java an array of Objects is actually an array of references to objects on the heap, so moving the references in the array has no effect on the underlying objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi! I think that's what I wanted to know. I wasn't sure if comparing the objects and moving them around within that array would mean changing the actual objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.