Is it a good option to use contains() method of ArrayList in nested for loops w.r.t performance?

Multi tool use
up vote
0
down vote
favorite
List<Employee> empsFromDB = repo.findAll(); //size m
List<Long> empIdsFromReq = req.getEmployeeIds();// size n
for(Employee emp: empsFromDB)
empIdsFromReq.contains(emp.getEmployeeId());
Is the above code optimum w.r.t
performance?
My approach has been to create a map of employee Ids
as key
and Employee as value and then retrieve the employees from map using the list of Ids.
My understanding is by using the second approach worst case is m+n
operations whereas in the first approach its m x n
, which I feel is not optimum.
Please advice.
java
add a comment |
up vote
0
down vote
favorite
List<Employee> empsFromDB = repo.findAll(); //size m
List<Long> empIdsFromReq = req.getEmployeeIds();// size n
for(Employee emp: empsFromDB)
empIdsFromReq.contains(emp.getEmployeeId());
Is the above code optimum w.r.t
performance?
My approach has been to create a map of employee Ids
as key
and Employee as value and then retrieve the employees from map using the list of Ids.
My understanding is by using the second approach worst case is m+n
operations whereas in the first approach its m x n
, which I feel is not optimum.
Please advice.
java
1
HashSet should perform better than List
– Pushpesh Kumar Rajwanshi
Nov 10 at 15:52
4
You're correct. A HashMap lookup is O(1). A list lookup is O(N). But the performance problem of that loop is probably negligible compared to loading all the employees from the database. That's what you should avoid. Unless of course you have very few employees, but then the loop will be fast enough anyway.
– JB Nizet
Nov 10 at 15:53
I'm not aware of your exact needs, but generally fetching all entities and filtering them in memory is a bad idea. You better ask DBMS to filter entities by ids for you.
– Nikolay Shevchenko
Nov 10 at 16:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
List<Employee> empsFromDB = repo.findAll(); //size m
List<Long> empIdsFromReq = req.getEmployeeIds();// size n
for(Employee emp: empsFromDB)
empIdsFromReq.contains(emp.getEmployeeId());
Is the above code optimum w.r.t
performance?
My approach has been to create a map of employee Ids
as key
and Employee as value and then retrieve the employees from map using the list of Ids.
My understanding is by using the second approach worst case is m+n
operations whereas in the first approach its m x n
, which I feel is not optimum.
Please advice.
java
List<Employee> empsFromDB = repo.findAll(); //size m
List<Long> empIdsFromReq = req.getEmployeeIds();// size n
for(Employee emp: empsFromDB)
empIdsFromReq.contains(emp.getEmployeeId());
Is the above code optimum w.r.t
performance?
My approach has been to create a map of employee Ids
as key
and Employee as value and then retrieve the employees from map using the list of Ids.
My understanding is by using the second approach worst case is m+n
operations whereas in the first approach its m x n
, which I feel is not optimum.
Please advice.
java
java
edited Nov 10 at 16:04


Md. Mokammal Hossen Farnan
484316
484316
asked Nov 10 at 15:50
Tushar Banne
323417
323417
1
HashSet should perform better than List
– Pushpesh Kumar Rajwanshi
Nov 10 at 15:52
4
You're correct. A HashMap lookup is O(1). A list lookup is O(N). But the performance problem of that loop is probably negligible compared to loading all the employees from the database. That's what you should avoid. Unless of course you have very few employees, but then the loop will be fast enough anyway.
– JB Nizet
Nov 10 at 15:53
I'm not aware of your exact needs, but generally fetching all entities and filtering them in memory is a bad idea. You better ask DBMS to filter entities by ids for you.
– Nikolay Shevchenko
Nov 10 at 16:19
add a comment |
1
HashSet should perform better than List
– Pushpesh Kumar Rajwanshi
Nov 10 at 15:52
4
You're correct. A HashMap lookup is O(1). A list lookup is O(N). But the performance problem of that loop is probably negligible compared to loading all the employees from the database. That's what you should avoid. Unless of course you have very few employees, but then the loop will be fast enough anyway.
– JB Nizet
Nov 10 at 15:53
I'm not aware of your exact needs, but generally fetching all entities and filtering them in memory is a bad idea. You better ask DBMS to filter entities by ids for you.
– Nikolay Shevchenko
Nov 10 at 16:19
1
1
HashSet should perform better than List
– Pushpesh Kumar Rajwanshi
Nov 10 at 15:52
HashSet should perform better than List
– Pushpesh Kumar Rajwanshi
Nov 10 at 15:52
4
4
You're correct. A HashMap lookup is O(1). A list lookup is O(N). But the performance problem of that loop is probably negligible compared to loading all the employees from the database. That's what you should avoid. Unless of course you have very few employees, but then the loop will be fast enough anyway.
– JB Nizet
Nov 10 at 15:53
You're correct. A HashMap lookup is O(1). A list lookup is O(N). But the performance problem of that loop is probably negligible compared to loading all the employees from the database. That's what you should avoid. Unless of course you have very few employees, but then the loop will be fast enough anyway.
– JB Nizet
Nov 10 at 15:53
I'm not aware of your exact needs, but generally fetching all entities and filtering them in memory is a bad idea. You better ask DBMS to filter entities by ids for you.
– Nikolay Shevchenko
Nov 10 at 16:19
I'm not aware of your exact needs, but generally fetching all entities and filtering them in memory is a bad idea. You better ask DBMS to filter entities by ids for you.
– Nikolay Shevchenko
Nov 10 at 16:19
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
If you can use a HashSet
instead of a List
you can improve performance.
contains()
for a HashSet
is O(1)
compared to O(n)
for a List
, therefore you should never use a List
if you can do it with a HashSet
.
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you can use a HashSet
instead of a List
you can improve performance.
contains()
for a HashSet
is O(1)
compared to O(n)
for a List
, therefore you should never use a List
if you can do it with a HashSet
.
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
2
down vote
If you can use a HashSet
instead of a List
you can improve performance.
contains()
for a HashSet
is O(1)
compared to O(n)
for a List
, therefore you should never use a List
if you can do it with a HashSet
.
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
2
down vote
up vote
2
down vote
If you can use a HashSet
instead of a List
you can improve performance.
contains()
for a HashSet
is O(1)
compared to O(n)
for a List
, therefore you should never use a List
if you can do it with a HashSet
.
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If you can use a HashSet
instead of a List
you can improve performance.
contains()
for a HashSet
is O(1)
compared to O(n)
for a List
, therefore you should never use a List
if you can do it with a HashSet
.
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 10 at 16:07


Boris the Spider
44.1k371119
44.1k371119
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Nov 10 at 15:58
Sand
4398
4398
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240627%2fis-it-a-good-option-to-use-contains-method-of-arraylist-in-nested-for-loops-w%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
WroJ6WHMbUkCv29nk90s,MB,AT
1
HashSet should perform better than List
– Pushpesh Kumar Rajwanshi
Nov 10 at 15:52
4
You're correct. A HashMap lookup is O(1). A list lookup is O(N). But the performance problem of that loop is probably negligible compared to loading all the employees from the database. That's what you should avoid. Unless of course you have very few employees, but then the loop will be fast enough anyway.
– JB Nizet
Nov 10 at 15:53
I'm not aware of your exact needs, but generally fetching all entities and filtering them in memory is a bad idea. You better ask DBMS to filter entities by ids for you.
– Nikolay Shevchenko
Nov 10 at 16:19