ArrayList與HashMap遍歷刪除元素,HashMap與ArrayList的clone體修改之間影響

前言

       最近做項目,需要一邊遍歷一邊刪除list與map,主要是ArrayList與HashMap。發現list與map刪除報錯了。而筆者同時需要保留舊的list與map,並執行增刪改操作時,使用克隆的方式,然而克隆map與list,發現引用對象在map或者list是淺克隆,即克隆引用或者指針。

筆者環境:Oracle JDK8

 

1. 遍歷刪除

 

1.1 ArrayList遍歷刪除

筆者查詢發現只能通過迭代器刪除。否則報錯java.util.ConcurrentModificationException。

public class IteratorTest {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");

        for (String str : list) {
            if ("222".equals(str)) {
                list.remove(str);
            }
        }
        System.out.println(list);
    }
}

筆者發現ArrayList的list.remove方法沒問題啊

public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

仔細發現modCount++; 這句代碼尤其要注意,這就是造成問題的誘因。

 

當筆者查看異常堆棧時

想起來foreach是執行迭代器語句,反編譯一下,果然,其實使用下標迭代是可以刪除的,ArrayList就是數組嘛,注意一下size判斷循環條件就可以了。

源碼分析,在ArrayList中,next方法,迭代器是ArrayList內部類實現的

這個check方法,很簡單,簡單粗暴拋異常。

final void checkForComodification() {
    if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

這個modCount是ArrayList的全局變量,而expectedModCount是迭代器初始化時copy一份當時的modCount,當remove時

modCount++;

而迭代器的值初始化就固定了,所以值不相等,拋異常了,next進行不下去了。

解決辦法:

①不用迭代器,下標刪除,注意刪除後size改變,判定條件也要改變

public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");

        for (int i = 0; i < list.size(); i++) {
            if ("222".equals(list.get(i))) {
                list.remove(i);
            }
        }
        System.out.println(list);
    }

②迭代器提供的刪除方法

public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");

        Iterator var2 = list.iterator();

        while(var2.hasNext()) {
            String str = (String)var2.next();
            if ("222".equals(str)) {
                var2.remove();
            }
        }
        System.out.println(list);
    }

解析源碼,迭代器會把modCount同步過來,expectedModCount = modCount;

public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

不過,頻繁增刪改的list不推薦使用ArrayList,LinkedList更爲方便,效率更高,當然這個問題仍然存在,還是要使用這2種方法處理。

1.2 HashMap同理

 

2. 克隆後增刪改

 

2.1 HashMap克隆

Person類省略

package com.feng.clone;

import java.util.HashMap;
import java.util.Map;

public class MapCloneTest {

    public static void main(String[] args) {
        HashMap<String, Person> map = new HashMap<>();
        Person p1 = new Person();
        p1.setName("tom");
        p1.setAge(12);

        Person p2 = new Person();
        p2.setAge(15);
        p2.setName("JIM");

        map.put("person1", p1);
        map.put("person2", p2);

        HashMap<String, Person> mapClone = (HashMap<String, Person>) map.clone();
        for (Map.Entry<String, Person> entry : map.entrySet()) {
            mapClone.get(entry.getKey()).setName("aaaaaaaa");
            mapClone.remove(entry.getKey());
            Person p3 = new Person();
            p3.setAge(88);
            p3.setName("kkkk");
            mapClone.put("person3", p3);

            System.out.println(entry.getKey() + "" + entry.getValue());
        }
    }
}

運行發現,克隆map修改會影響本體map的bean,增加刪除不會影響本體map

Connected to the target VM, address: '127.0.0.1:51147', transport: 'socket'
person2Person(name=aaaaaaaa, age=15)
person1Person(name=aaaaaaaa, age=12)
Disconnected from the target VM, address: '127.0.0.1:51147', transport: 'socket'

 

原理分析

看看HashMap的clone方法

public Object clone() {
        HashMap<K,V> result;
        try {
            //1.clone
            result = (HashMap<K,V>)super.clone();
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
        //2.reinit
        result.reinitialize();
        //3.copy table[]
        result.putMapEntries(this, false);
        return result;
    }

void reinitialize() {
        table = null;
        entrySet = null;
        keySet = null;
        values = null;
        modCount = 0;
        threshold = 0;
        size = 0;
    }

可以看出

①直接clone的hashmap

②重新初始化

③複製本體map的table[]數組

    /**
     * Implements Map.putAll and Map constructor.
     *
     * @param m the map
     * @param evict false when initially constructing this map, else
     * true (relayed to method afterNodeInsertion).
     */
    final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
        int s = m.size();
        if (s > 0) {
            if (table == null) { // pre-size
                float ft = ((float)s / loadFactor) + 1.0F;
                int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                         (int)ft : MAXIMUM_CAPACITY);
                if (t > threshold)
                    threshold = tableSizeFor(t);
            }
            else if (s > threshold)
                resize();
            //循環遍歷設置,但是隻是引用,對象並未深度克隆
            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
                K key = e.getKey();
                V value = e.getValue();
                putVal(hash(key), key, value, false, evict);
            }
        }
    }

 

2.2 ArrayList克隆

package com.feng.clone;

import java.util.ArrayList;

public class ArrayListClone {
    public static void main(String[] args) {
        ArrayList<Person> list = new ArrayList<>();
        Person p1 = new Person();
        p1.setName("tom");
        p1.setAge(12);

        Person p2 = new Person();
        p2.setAge(15);
        p2.setName("JIM");
        list.add(p1);
        list.add(p2);

        ArrayList<Person> listClone = (ArrayList<Person>) list.clone();
        for (Person p : list) {
            listClone.get(0).setName("aaaaaaaa");
            listClone.remove(p);
            Person p3 = new Person();
            p3.setAge(88);
            p3.setName("kkkk");
            listClone.add(p3);

            System.out.println(p);
        }
    }
}

運行示例,可以看出跟map相同的現象,僅是arraylist克隆,對象只克隆引用

Person(name=aaaaaaaa, age=12)
Person(name=aaaaaaaa, age=15)

Process finished with exit code 0

ArrayList的clone原理分析

public Object clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }

很簡單,直接克隆arraylist,然後複製數組,裏面的元素僅複製引用

 

總結

       筆者在使用HashMap和ArrayList遍歷刪除元素的時候,也想到了copOnWriteList,但是Map沒法實現,並且筆者的需求有新舊map和list只能修改新的map或者list,所以想到了克隆,但是HashMap和ArrayList的元素bean未克隆,筆者必須取出bean,然後對bean克隆然後設置到新的Map或者list中,HashMap和ArrayList是淺克隆,深克隆推薦序列化。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章