List Map

Input: let them say that I have a Person object. It has 2 properties, namely

  • ssnNo - social security number
  • name .

In one hand, I have a List of Person objects (with a unique ssnNo), and on the other hand, I have a Map containing Person ssnNo as a key and Person name as a value.

Conclusion: I need character names using its ssnNo.

Questions:

  • Which approach should be followed from the 2 mentioned above, for example, using a list or map? (I think the map will be the obvious answer).

  • If it is a map, is it always recommended to use a map, a large or small data set? I mean any performance issues related to the card.

+4
source share
6 answers

The map is the way to go. Maps work very well, and their advantages over search lists are becoming greater, the larger your dataset.

Of course, there are some important performance considerations:

  • Make sure you have a good hashcode implementation (and the corresponding one is equal), so that your data will be evenly distributed across the map codes.

  • Make sure you preconfigure your card when placing it (if at all possible). The map will automatically resize, but the resize operation essentially requires re-installing each previous item in a new, larger map.

+3
source

You are right, you must use the card in this case. There is no performance problem using the map compared to lists, the performance is much better than the list when the data is large. A map uses key hash codes to retrieve records, similar to how arrays use indexes to retrieve values, which gives good performance

0
source

This looks like a situation suitable for Map<Long, Person> , which maps the social security number to the corresponding Person . You might want to remove the ssnNo field from Person to avoid any layoffs (since you will store these values โ€‹โ€‹as keys on your card).

In general, Map and List are very different structures, each of which is suitable for different circumstances. You would use the first one when you want to maintain a set of key-value pairs, which allows you to easily and quickly (i.e., in constant time) search for values โ€‹โ€‹based on keys (this is what you want to do). You would use the latter when you just want to keep an ordered, linear set of elements.

0
source

I think it makes sense to have a Person object, but it also makes sense to use a Map over a List , since the search time will be faster. I would probably use a Map with SSNs as keys and Person objects as values :

  Map<SSN,Person> ssnToPersonMap; 
0
source

All pointers. Actually, it makes no sense to have Map<ssn,PersonName> instead of Map<ssn,Person> . The latter is the best choice most of the time.

0
source

Using a map, especially using a hash table, will be faster than a list, as this will allow you to get the name at constant O (1) time. However, using a list, you need to do a linear search, or maybe a binary search, which is slower.

0
source

Source: https://habr.com/ru/post/1490144/


All Articles