Android map研究

82 篇文章 1 订阅
3 篇文章 0 订阅

单线程存放10000条数据,hashMap、ConcurrentHashMap、hashMap + synchronized耗时分析

public class MainActivity extends AppCompatActivity {
    private String TAG = "MapTest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                return thread;
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2),
                factory, new ThreadPoolExecutor.AbortPolicy());

        executor.execute(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> hashMap = new HashMap<>();
                long hashMapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    hashMap.put(Thread.currentThread().getName() + "-" +i,Thread.currentThread().getName() + "-" +i);
                }
                long hashMapEndTime = System.currentTimeMillis();
                Log.e(TAG, "hashMap put "+ hashMap.size()+" values cost time: " + (hashMapEndTime - hashMapStartTime));
            }
        });


        executor.execute(new Runnable() {
            @Override
            public void run() {
                ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<String,String>();
                long mapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    concurrentHashMap.put(Thread.currentThread().getName() + "-" +i,Thread.currentThread().getName() + "-" +i);
                }
                long mapEndTime = System.currentTimeMillis();
                Log.e(TAG, "concurrentHashMap put "+ concurrentHashMap.size()+" values cost time: " + (mapEndTime - mapStartTime));
            }
        });

        Object o = new Object();
        // hashMap + synchronized 锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> hashMap = new HashMap<>();
                long hashMapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    synchronized (o){
                        hashMap.put(Thread.currentThread().getName() + "-" +i,Thread.currentThread().getName() + "-" +i);
                    }
                }
                long hashMapEndTime = System.currentTimeMillis();
                Log.e(TAG, "hashMap + synchronized put "+ hashMap.size()+" values cost time: " + (hashMapEndTime - hashMapStartTime));

            }
        });

    }
}

耗时结果:

2022-07-17 09:43:54.950 5497-5526/com.example.myapplication E/MapTest: hashMap + synchronized put 10000 values cost time: 140
2022-07-17 09:43:54.952 5497-5524/com.example.myapplication E/MapTest: hashMap put 10000 values cost time: 144
2022-07-17 09:43:54.953 5497-5525/com.example.myapplication E/MapTest: concurrentHashMap put 10000 values cost time: 144

三线程存放30000条数据,每个线程存放10000数据、hashMap、ConcurrentHashMap、hashMap + synchronized耗时分析

public class MainActivity2 extends AppCompatActivity {
    private String TAG = "MapTest";
    private HashMap<String, String> hashMap;
    private ConcurrentHashMap<String, String> concurrentHashMap;
    private long mapStartTime;
    private long hashMapStartTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                return thread;
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2),
                factory, new ThreadPoolExecutor.AbortPolicy());


        hashMap = new HashMap<>();
        hashMapStartTime = System.currentTimeMillis();
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        hashMap.put(Thread.currentThread().getName() + "-" + j, Thread.currentThread().getName() + "-" + j);
                    }
                    long hashMapEndTime = System.currentTimeMillis();
                    Log.e(TAG, "hashMap put " + hashMap.size() + " values cost time: " + (hashMapEndTime - hashMapStartTime));
                }
            });
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        concurrentHashMap = new ConcurrentHashMap<String,String>();
        mapStartTime = System.currentTimeMillis();
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        concurrentHashMap.put(Thread.currentThread().getName() + "-" + j, Thread.currentThread().getName() + "-" + j);
                    }
                    long mapEndTime = System.currentTimeMillis();
                    Log.e(TAG, "concurrentHashMap put " + concurrentHashMap.size() + " values cost time: " + (mapEndTime - mapStartTime));
                }
            });
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Object o = new Object();
        hashMap = new HashMap<>();
        hashMapStartTime = System.currentTimeMillis();
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        synchronized (o){
                            hashMap.put(Thread.currentThread().getName() + "-" + j, Thread.currentThread().getName() + "-" + j);
                        }
                    }
                    long hashMapEndTime = System.currentTimeMillis();
                    Log.e(TAG, "hashMap + synchronized put " + hashMap.size() + " values cost time: " + (hashMapEndTime - hashMapStartTime));
                }
            });
        }


    }
}
2022-07-17 09:55:06.863 6155-6182/com.example.myapplication E/MapTest: hashMap put 27663 values cost time: 72
2022-07-17 09:55:06.864 6155-6183/com.example.myapplication E/MapTest: hashMap put 28057 values cost time: 73
2022-07-17 09:55:06.866 6155-6184/com.example.myapplication E/MapTest: hashMap put 29205 values cost time: 74
2022-07-17 09:55:07.853 6155-6185/com.example.myapplication E/MapTest: concurrentHashMap put 22030 values cost time: 59
2022-07-17 09:55:07.872 6155-6183/com.example.myapplication E/MapTest: concurrentHashMap put 29407 values cost time: 78
2022-07-17 09:55:07.873 6155-6182/com.example.myapplication E/MapTest: concurrentHashMap put 30000 values cost time: 78
2022-07-17 09:55:08.932 6155-6186/com.example.myapplication E/MapTest: hashMap + synchronized put 28818 values cost time: 135
2022-07-17 09:55:08.932 6155-6184/com.example.myapplication E/MapTest: hashMap + synchronized put 29086 values cost time: 136
2022-07-17 09:55:08.934 6155-6185/com.example.myapplication E/MapTest: hashMap + synchronized put 30000 values cost time: 138

第一组是对比concurrentHashMap、hashMap + synchronized中存放10000个数据的耗时:
第二组是对比hashMap、concurrentHashMap、hashMap + synchronized覆盖数据量大小为10的map中的某一个key_value 10000次的耗时结果:
第三组是对比hashMap、concurrentHashMap、hashMap + synchronized分别使用三个线程存放数据,每个线程存储10000条数据的耗时结果:

public class MainActivity extends AppCompatActivity {
    private String TAG = "MapTest";
    private HashMap hashMap;
    private long hashMapStartTime;
    private ConcurrentHashMap concurrentHashMap;
    private long mapStartTime;
    private Object o = new Object();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                return thread;
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 4, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2),
                factory, new ThreadPoolExecutor.AbortPolicy());

        executor.execute(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> hashMap = new HashMap<>();
                long hashMapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    hashMap.put(Thread.currentThread().getName() + "-" + i, Thread.currentThread().getName() + "-" + i);
                }
                long hashMapEndTime = System.currentTimeMillis();
                Log.e(TAG, "hashMap put " + hashMap.size() + " values cost time: " + (hashMapEndTime - hashMapStartTime));
            }
        });


        executor.execute(new Runnable() {
            @Override
            public void run() {
                ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>();
                long mapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    concurrentHashMap.put(Thread.currentThread().getName() + "-" + i, Thread.currentThread().getName() + "-" + i);
                }
                long mapEndTime = System.currentTimeMillis();
                Log.e(TAG, "concurrentHashMap put " + concurrentHashMap.size() + " values cost time: " + (mapEndTime - mapStartTime));
            }
        });

        // hashMap + synchronized 锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> hashMap = new HashMap<>();
                long hashMapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    synchronized (o) {
                        hashMap.put(Thread.currentThread().getName() + "-" + i, Thread.currentThread().getName() + "-" + i);
                    }
                }
                long hashMapEndTime = System.currentTimeMillis();
                Log.e(TAG, "hashMap + synchronized put " + hashMap.size() + " values cost time: " + (hashMapEndTime - hashMapStartTime));

            }
        });

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        executor.execute(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("1", "1");
                hashMap.put("2", "1");
                hashMap.put("3", "1");
                hashMap.put("4", "1");
                hashMap.put("5", "1");
                hashMap.put("6", "1");
                hashMap.put("7", "1");
                hashMap.put("8", "1");
                hashMap.put("9", "1");
                hashMap.put("10", "1");
                long hashMapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    hashMap.put("1", "1");
                }
                long hashMapEndTime = System.currentTimeMillis();
                Log.e(TAG, "hashMap overWrite " + 10000 + " times cost time: " + (hashMapEndTime - hashMapStartTime));
            }
        });


        executor.execute(new Runnable() {
            @Override
            public void run() {
                ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>();
                concurrentHashMap.put("1", "1");
                concurrentHashMap.put("2", "1");
                concurrentHashMap.put("3", "1");
                concurrentHashMap.put("4", "1");
                concurrentHashMap.put("5", "1");
                concurrentHashMap.put("6", "1");
                concurrentHashMap.put("7", "1");
                concurrentHashMap.put("8", "1");
                concurrentHashMap.put("9", "1");
                concurrentHashMap.put("10", "1");
                long mapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    concurrentHashMap.put("1", "1");
                }
                long mapEndTime = System.currentTimeMillis();
                Log.e(TAG, "concurrentHashMap overWrite " + 10000 + " times cost time: " + (mapEndTime - mapStartTime));
            }
        });

        // hashMap + synchronized 锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("1", "1");
                hashMap.put("2", "1");
                hashMap.put("3", "1");
                hashMap.put("4", "1");
                hashMap.put("5", "1");
                hashMap.put("6", "1");
                hashMap.put("7", "1");
                hashMap.put("8", "1");
                hashMap.put("9", "1");
                hashMap.put("10", "1");
                long hashMapStartTime = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    synchronized (o) {
                        hashMap.put("1", "1");
                    }
                }
                long hashMapEndTime = System.currentTimeMillis();
                Log.e(TAG, "hashMap + synchronized overWrite " + 10000 + " times cost time: " + (hashMapEndTime - hashMapStartTime));

            }
        });


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        concurrentHashMap = new ConcurrentHashMap<String, String>();
        mapStartTime = System.currentTimeMillis();
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        concurrentHashMap.put(Thread.currentThread().getName() + "-" + j, Thread.currentThread().getName() + "-" + j);
                    }
                    long mapEndTime = System.currentTimeMillis();
                    Log.e(TAG, "concurrentHashMap put " + concurrentHashMap.size() + " values cost time: " + (mapEndTime - mapStartTime));
                }
            });
        }


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        hashMap = new HashMap<String, String>();
        hashMapStartTime = System.currentTimeMillis();
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        hashMap.put(Thread.currentThread().getName() + "-" + j, Thread.currentThread().getName() + "-" + j);
                    }
                    long hashMapEndTime = System.currentTimeMillis();
                    Log.e(TAG, "hashMap put " + hashMap.size() + " values cost time: " + (hashMapEndTime - hashMapStartTime));
                }
            });
        }


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        hashMap = new HashMap<>();
        hashMapStartTime = System.currentTimeMillis();
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        synchronized (o) {
                            hashMap.put(Thread.currentThread().getName() + "-" + j, Thread.currentThread().getName() + "-" + j);
                        }
                    }
                    long hashMapEndTime = System.currentTimeMillis();
                    Log.e(TAG, "hashMap + synchronized put " + hashMap.size() + " values cost time: " + (hashMapEndTime - hashMapStartTime));
                }
            });
        }

    }
}
2022-07-17 10:14:33.144 7277-7304/? E/MapTest: concurrentHashMap put 10000 values cost time: 103
2022-07-17 10:14:33.153 7277-7303/? E/MapTest: hashMap put 10000 values cost time: 112
2022-07-17 10:14:33.154 7277-7305/? E/MapTest: hashMap + synchronized put 10000 values cost time: 106
2022-07-17 10:14:38.048 7277-7303/? E/MapTest: concurrentHashMap overWrite 10000 times cost time: 4
2022-07-17 10:14:38.049 7277-7304/? E/MapTest: hashMap overWrite 10000 times cost time: 5
2022-07-17 10:14:38.051 7277-7305/? E/MapTest: hashMap + synchronized overWrite 10000 times cost time: 6
2022-07-17 10:14:43.084 7277-7304/? E/MapTest: concurrentHashMap put 24568 values cost time: 38
2022-07-17 10:14:43.089 7277-7307/? E/MapTest: concurrentHashMap put 29046 values cost time: 43
2022-07-17 10:14:43.091 7277-7303/? E/MapTest: concurrentHashMap put 30000 values cost time: 45
2022-07-17 10:14:48.078 7277-7307/? E/MapTest: hashMap put 17009 values cost time: 28
2022-07-17 10:14:48.090 7277-7305/? E/MapTest: hashMap put 25622 values cost time: 41
2022-07-17 10:14:48.097 7277-7309/? E/MapTest: hashMap put 29355 values cost time: 48
2022-07-17 10:14:53.203 7277-7305/? E/MapTest: hashMap + synchronized put 23495 values cost time: 151
2022-07-17 10:14:53.222 7277-7311/? E/MapTest: hashMap + synchronized put 29300 values cost time: 170
2022-07-17 10:14:53.223 7277-7303/? E/MapTest: hashMap + synchronized put 30000 values cost time: 171

2022-07-17 10:35:54.819 8561-8590/com.example.myapplication E/MapTest: concurrentHashMap put 10000 values cost time: 143
2022-07-17 10:35:54.824 8561-8591/com.example.myapplication E/MapTest: hashMap + synchronized put 10000 values cost time: 143
2022-07-17 10:35:54.827 8561-8589/com.example.myapplication E/MapTest: hashMap put 10000 values cost time: 152
2022-07-17 10:35:59.683 8561-8591/com.example.myapplication E/MapTest: concurrentHashMap overWrite 10000 times cost time: 3
2022-07-17 10:35:59.684 8561-8590/com.example.myapplication E/MapTest: hashMap overWrite 10000 times cost time: 4
2022-07-17 10:35:59.688 8561-8589/com.example.myapplication E/MapTest: hashMap + synchronized overWrite 10000 times cost time: 8
2022-07-17 10:36:04.728 8561-8590/com.example.myapplication E/MapTest: concurrentHashMap put 22345 values cost time: 48
2022-07-17 10:36:04.740 8561-8591/com.example.myapplication E/MapTest: concurrentHashMap put 26892 values cost time: 60
2022-07-17 10:36:04.754 8561-8596/com.example.myapplication E/MapTest: concurrentHashMap put 30000 values cost time: 74
2022-07-17 10:36:09.737 8561-8634/com.example.myapplication E/MapTest: hashMap put 20812 values cost time: 56
2022-07-17 10:36:09.763 8561-8589/com.example.myapplication E/MapTest: hashMap put 28044 values cost time: 77
2022-07-17 10:36:09.772 8561-8591/com.example.myapplication E/MapTest: hashMap put 29790 values cost time: 89
2022-07-17 10:36:14.916 8561-8635/com.example.myapplication E/MapTest: hashMap + synchronized put 24728 values cost time: 233
2022-07-17 10:36:14.936 8561-8589/com.example.myapplication E/MapTest: hashMap + synchronized put 28907 values cost time: 253
2022-07-17 10:36:14.938 8561-8596/com.example.myapplication E/MapTest: hashMap + synchronized put 30000 values cost time: 255

HashMap Remove

remove() 方法用于删除hashMap 中指定键 key 对应的键值对(key-value)。

返回值
如果指定 key,返回指定键 key 关联的值,如果指定的 key 映射值为 null 或者该 key 并不存在于该 HashMap 中,此方法将返回null。
如果指定了 key 和 value,删除成功返回 true,否则返回 false。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学知识拯救世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值