HashSet vs ArrayList contains performance

前端 未结 5 705
一整个雨季
一整个雨季 2020-12-14 05:55

When processing large amounts of data I often find myself doing the following:

HashSet set = new HashSet ();
//Adding elements to         


        
5条回答
  •  甜味超标
    2020-12-14 06:21

    The set will give much better performance (O(n) vs O(n^2) for the list), and that's normal because set membership (the contains operation) is the very purpose of a set.

    Contains for a HashSet is O(1) compared to O(n) for a list, therefore you should never use a list if you often need to run contains.

提交回复
热议问题