2

I have been wondering what is the actual benefit of using Lists. Please note that my question is not "when to use what" but rather Is there any impact on performance if i insist on having maps as my primary objects

obviously if my aim is to just work on values

UPDATE after not being clear with my intent at first glance: I meant if i just want to filter a list of [8000] people whose age is > 30 , i would use a list... But can i use a map instead and have it be used instead - My Question is - will there be any performance hindrance ?

I would also use List. But do we get any performance boost - if yes - How can I see it myself.

for example if i take

List <Integer> listOfInt = new ArrayList<>(map.values());

It would make sense to use Map as my global object and serve lists based on it.

I know the key/value O(1) runtime for insert or remove in Maps but then why Lists are preferred most places i have seen.

11
  • 1
    You question doesn't make any sense because it largely depends on the use case. If you don't describe what "to just work with values" means nobody can tell. Commented Oct 4, 2017 at 10:04
  • I meant if i just want to filter a list of people whose age is > 30 , i would use a list... But can i use a map instead and have it be used instead - My Question is - will there be any performance hindrance ? Commented Oct 4, 2017 at 10:05
  • How many values are in your list/map? Commented Oct 4, 2017 at 10:06
  • 1
    I don't think so. Looping over 8000 elements will be so fast that you won't see much of a difference. Just write a benchmark an try with 800.000 entries. Commented Oct 4, 2017 at 10:10
  • 1
    @Saurabh Make stress tests with map and list and measure the time. (for example long startTime = System.nanoTime(); create a loop with 10.000.000 entries and then measure time again. Commented Oct 4, 2017 at 10:11

2 Answers 2

9

my question is not "when to use what"

but it should. List and Map are having different use. A List is - well - a list of values without any explicit key. Item in a list is designated by its position.

obviously if my aim is to just work on values I would also use List.

yes, that's correct

But do we get any performance boost

Please note,. the Map is not a simple structure. For each item in a map, an "Entry" object is created with references to the key and the value object, an hash array is created, etc.. so using map you definitely use more memory and code. For simpler cases the performance difference is negligible

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

Comments

1

It depends on the use case. With your example it could make a difference in the usage for example if you want access a specific object. The access time with a List would be O(n) while in a Map it is O(1).

If you don't care about specific retrieval of objects you can use a List.

10 Comments

Thanks, my aim is to provide filtering capabilities (updated the question with it).
I don't agree. An item inside a list can be accessed in O(1) if you know the position. Access in MAp is O(log(n)) or similar, because the key needs to be looked-up.
Access in a map is O(logN) ? how ?
@gusto2 you are wrong. It should be O(1), but for HashMaps it could happen to be O(n) worst case check stackoverflow.com/questions/4553624/hashmap-get-put-complexity
@MehdiB. well - O(1) you can expect on small (1 level) maps, however if there are ~ 10^3 or 10^4 records with uniform distribution you will get O(log(n)) as claims the answer in your link. Regardless that, if Map is used instead of List, it just creates additional structures and relations.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.