慎用java集合中的clear()方法

一、不多说了,直接上代码和结果:

1.代码

package com.xbl.rocketMq.consumer;

import org.bson.Document;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2018-02-01.
 */
public class T {
    public static void main(String[] args) {
        List> list = new ArrayList>();
        Map map = new HashMap();
        for(int i=0;i<3;i++){
            map.put(""+i,""+i+100);
            list.add(map);
        }
        System.out.println("list的长度:"+list.size());
        System.out.println("list.get(0):"+list.get(0));
        System.out.println("list.get(1):"+list.get(1));
        System.out.println("list.get(2):"+list.get(2));
        
        System.out.println("===============");
        
        System.out.println("开始执行:map.clear()");
        map.clear();
        System.out.println("开始执行:map.put(\"222\",\"222222222\")");
        map.put("222","222222222");//************
        System.out.println("list的长度:"+list.size());
        System.out.println("list.get(0):"+list.get(0));
        System.out.println("list.get(1):"+list.get(1));
        System.out.println("list.get(2):"+list.get(2));
        
        System.out.println("--------------------------------");
        
        List w_c_data = new ArrayList();
        Document middle = new Document();
        for(int i=0;i<3;i++){
            middle.put("want_content_id",""+i);
            middle.put("want_content_name",""+i);
            w_c_data.add(middle);
        }
        middle.put("111","111111111111");//************
        System.out.println("w_c_data的长度:"+w_c_data.size()+"***");
        System.out.println(w_c_data.get(0)+"%%%%");
        System.out.println(w_c_data.get(1)+"%%%%");
        System.out.println(w_c_data.get(2)+"%%%%");
        
        System.out.println("================");
        
        System.out.println("开始执行:middle.clear()");
        middle.clear();//************
        System.out.println("开始执行: middle.put(\"111\",\"111111111111\")");
        middle.put("111","111111111111");
        System.out.println("w_c_data.get(0): " + w_c_data.get(0)+"%%%%");
        System.out.println("w_c_data.get(1): " + w_c_data.get(1)+"%%%%");
        System.out.println("w_c_data.get(2): " + w_c_data.get(2)+"%%%%");

    }
}
2.结果:

list的长度:3
list.get(0):{2=2100, 1=1100, 0=0100}
list.get(1):{2=2100, 1=1100, 0=0100}
list.get(2):{2=2100, 1=1100, 0=0100}
===============
开始执行:map.clear()
开始执行:map.put("222","222222222")
list的长度:3
list.get(0):{222=222222222}
list.get(1):{222=222222222}
list.get(2):{222=222222222}
--------------------------------
w_c_data的长度:3***
Document{{want_content_id=2, want_content_name=2, 111=111111111111}}%%%%
Document{{want_content_id=2, want_content_name=2, 111=111111111111}}%%%%
Document{{want_content_id=2, want_content_name=2, 111=111111111111}}%%%%
================
开始执行:middle.clear()
开始执行: middle.put("111","111111111111")
w_c_data.get(0): Document{{111=111111111111}}%%%%
w_c_data.get(1): Document{{111=111111111111}}%%%%
w_c_data.get(2): Document{{111=111111111111}}%%%%

Process finished with exit code 0


你可能感兴趣的:(java)