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









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.










share|improve this question



















  • 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














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.










share|improve this question



















  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer










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.

















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer










    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.





















      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.






      share|improve this answer










      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.



















        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.






        share|improve this answer










        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.







        share|improve this answer










        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.









        share|improve this answer



        share|improve this answer








        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.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線