본문 바로가기

:::::: STUDY ::::::/Android

[Android] ArrayList에서의 clear()와 removeAll() 차이점

[Android] ArrayList에서의 clear()와 removeAll() 차이점


ArrayList에 있는 내용을 삭제할때 쓰이는 clear()와 removeAll()..


-----------------------------------

clear() - source

    @Override public void clear() {

        if (size != 0) {

            Arrays.fill(array, 0, size, null);

            size = 0;

            modCount++;

        }

    }

-----------------------------------

removeAll() - source

    public boolean removeAll(Collection<?> collection) {

        boolean result = false;

        Iterator<?> it = iterator();

        while (it.hasNext()) {

            if (collection.contains(it.next())) {

                it.remove();

                result = true;

            }

        }

        return result;

    }

-----------------------------------


소스에서 보이듯이... 속도에서도 차이가 보인다.

특정케이스가 아니라면 clear() 쓰는게 답일듯


removeAll()은 A.removeAll(B)라고 쓰이며,

A에서 B에 있는 것을 삭제 할 때 사용.


':::::: STUDY :::::: > Android' 카테고리의 다른 글

[Android] Handler에서 lint warning 제거  (0) 2014.02.12
Full screen에 투명 activity로 Style 바꾸기  (0) 2013.12.04
Failed to allocate memory: 8  (0) 2013.10.24
Bitmap & BitmapDrawable  (0) 2013.09.12
화면 크기 구하기  (0) 2013.09.11