zoukankan      html  css  js  c++  java
  • Java集合篇四:Map的基本应用

    package com.test.collection;
    
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    //Map的基本应用
    public class MapTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            /* 
             * HashMap KEY值不允许重复,线程不安全,效率高
             */
            Map map=new HashMap();
            map.put(1, "张三");
            map.put(2, "李四");
            map.put(3, "王五");
            map.remove(2);
            System.out.println(map.get(2));
            System.out.println(map.size());
            
            /* 
             * HashTable 线程安全,效率低
             */
            Map map2=new Hashtable<>();
            map2.put(1, "张三");
            map2.put(2, "李四");
            map2.put(3, "王五");
            map2.remove(2);
            System.out.println(map2.get(2));
            System.out.println(map2.size());
            
            /**ConcurrentHashMap 
             * 
             * 底层其实是由多个小的hash table组织,线程同步时,可以分段锁
             * 
             * 比如:当我们希望线程同步,但是又不希望效率和hashTable一样太低,可以使用ConcurrentHashMap
             */
            ConcurrentHashMap cutMap=new ConcurrentHashMap<>();
            cutMap.put("123", "brant");
            cutMap.put("456", "jack");
            System.out.println(cutMap.get("123"));
        }
    
    }
  • 相关阅读:
    Android笔记
    Scala中apply的用法
    MySQL备忘
    Spring test
    Scala
    Dubbo
    Scala元组
    Scala中None, Nil, Nothing的区别
    java多态与异常处理——动手动脑
    《大道至简》第七八章读后感
  • 原文地址:https://www.cnblogs.com/brant/p/6231205.html
Copyright © 2011-2022 走看看