0

This is a somewhat academic question about java efficiency wrt collections. Assume there are two classes SearchForm and Manager. SearchForm has a Collection of Persons and calls Manager to find out if a Person has a subscription, it will then iterate over persons to find their subscription status. Object Person has an int id (Person.getId()). Which is more energy-efficient in Java:

Map<Integer, Boolean> MapPersonSubscription(Collection<Person> persons, Date date, int subscriptionId);

Map<Person, Boolean> MapPersonSubscription(Collection<Person> persons, Date date, int subscriptionId);

The collection of Persons is already in memory, does it cost extra (in some way) to use its elements as Map keys in the returned map? Is it 'cheaper' to return a map using the smaller key (int) but perform Person.getId in the calling class when retrieving each persons value from the map?

This is an toy example, assume Manager needs Person, not just Person.getId to find the subscription and it is preferable to query a subsciption for all persons at once because of the underlying db query.

Related question: Most efficient key object type in a hash map?

Ivana
  • 643
  • 10
  • 27
  • Map keys are just references to objects on the heap so if the person instance already exists it's just an additional reference. The integer for the id might be a new object if the person's id is just an `int`. However, I would not bother with memory questions like this unless you _really_ have a problem and then just changing the key type would probably not be sufficient. ... – Thomas Nov 23 '22 at 09:51
  • This is not generally answerable. It depends on the implementation of equals and hashCode of the object you use as a key. – Mark Rotteveel Nov 23 '22 at 09:51
  • 2
    I'd rather think about practical development questions: Are any of the fields used by `Person.hashCode()` and `Person.equals()` mutable? Yes: don't use `Person` as a key. Does especially `equals()` need to check more than the id? Yes: don't use `Person` as a key. Do you have situations where you only have a person's id and still want to retrieve the data? Yes: better use the id as the key rather than having to lookup a person or create a dummy just to compare the ids. – Thomas Nov 23 '22 at 09:52
  • Btw, "Is it 'cheaper' to return a map using the smaller key (int) but perform Person.getId in the calling class when retrieving each persons value from the map?" - I'd say that doesn't even matter. The resulting instructions would probably be very similar which means we're probably talking about a difference of 1 or 2 CPU instructions. – Thomas Nov 23 '22 at 09:56
  • One advantage of using `Person` is that it makes it clear what the key is, so it's easier for other people to understand your program. – tgdavies Nov 23 '22 at 09:56
  • @tgdavies yes, thanks for spotting this. corrected :) – Thomas Nov 23 '22 at 09:57
  • Since you are doing a DB query, that will dominate performance to the point that the performance of the Map is likely to be irrelevant. – tgdavies Nov 23 '22 at 09:59

0 Answers0