HashSet vs ArrayList contains performance

前端 未结 5 704
一整个雨季
一整个雨季 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:25

    If you don't need a list, I would just use a Set and this is the natural collection to use if order doesn't matter and you want to ignore duplicates.

    You can do both is you need a List without duplicates.

    private Set set = new HashSet<>();
    private List list = new ArrayList<>();
    
    
    public void add(String str) {
        if (set.add(str))
            list.add(str);
    }
    

    This way the list will only contain unique values, the original insertion order is preserved and the operation is O(1).

提交回复
热议问题