JAVA集合之map

目录

简介

HashMap

TreeMap

 Hashtable

JAVA并发容器ConcurrentSkipListMap和ConcurrentHashMap实现线程安全


简介

|————Map:双列数据,存储key-value对的数据,
         |————HashMap:作为Map主要实现类,线程不安全的,效率高,存储null的key和value
                  |————LinkedHashMap:保证遍历元素时,按照添加顺序实现遍历
                          原理:在原有HashMap底层结构基础上,添加一堆指针,频繁遍历操作,执行效率高于HashMap
         |————TreeMap:保证按照添加的key-value进行排序,实现排序遍历,此时考虑key的自然排序和定制排序,底层使用的是红黑树
         |————Hashtable:作为古老的实现类,线程安全的,效率低,不能存储null的key和value
                  |————Properties:常用来处理配置文件,key和value都是String类型

Map结构
   Map中的key:无序的、不可重复的,使用Set存储所有的key --->key所在的类要重写equals()和hashCode()(以HashMap为例
   Map中的value:无序的、可重复的,使用Collection存储所有的value   value所在类重写equals()
   一个键值对:key-value构成一个Entry对象
   Map中的entry:无序的,不可重复的,使用Set存储所有的entry

将Map中的数据,通过迭代器输出:Map —> Set  —> Iterator

  • 调用Map.entrySet()方法,将Map集合转换为Set集合,其中Set中的泛型为:Map.Entry<K,V>;
  • 调用Set.iterator()方法,将Set集合转换为Iterator迭代器,泛型依然是:Map.Entry<K,V>;
  • 调用Iterator的hasNext()、next()方法,循环取出Map.Entry<K,V>接口对象;
  • 调用Map.Entry接口的getKey()获取key、getValue()获取value。

=========================================================================

HashMap

HashMap的底层:jdk7及以前    数组加链表  
                              jdk8及以后    数组加链表加红黑树

 

=========================================================================

TreeMap

因TreeMap中添加key-value,要求key必须由同一个类创建的对象
因为要按照key进行排序,自然排序、定制排序

因TreeMap中添加key-value,要求key必须由同一个类创建的对象
因为要按照key进行排序,自然排序、定制排序

  @Test
    public void treeMapTest1() {
        TreeMap map = new TreeMap((o1, o2) -> {
            if (o1 instanceof StudentSet && o2 instanceof StudentSet) {
                StudentSet s1 = (StudentSet) o1;
                StudentSet s2 = (StudentSet) o2;
                return Integer.compare(s1.getAge(), s2.getAge());
            } else {
                throw new RuntimeException("类型错误");
            }
        });

        StudentSet st1 = new StudentSet(13, "lucky");
        StudentSet st2 = new StudentSet(10, "jack");
        StudentSet st3 = new StudentSet(23, "Tom");
        map.put(st1, 89);
        map.put(st2, 78);
        map.put(st3, 100);
//        方式一
        Set entrySet = map.entrySet();
        Iterator a = entrySet.iterator();
        while (a.hasNext()) {
            Object obj = a.next();
            Map.Entry entry = (Map.Entry) obj;
            System.out.println(entry.getKey() + "---->" + entry.getValue());
        }

//        方式二
        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value = map.get(key);
            System.out.println(key + "----" + value);
        }

    }

 =========================================================================

 Hashtable

HashTable中hash数组默认大小是11,增加的方式是 old*2+1

HashTable是将整个哈希表锁住,采用sychronized同步方法,所以性能很低;

  @Test
    public void propertiesTest()  {
        FileInputStream fileInputStream=null;
        try {
            Properties properties = new Properties();
            fileInputStream= new FileInputStream("jabc.properties");
            //加载流对应的文件
            properties.load(fileInputStream);

            String name=properties.getProperty("name");
            String password=properties.getProperty("password");
            System.out.println(name+"---"+password);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

JAVA并发容器ConcurrentSkipListMap和ConcurrentHashMap实现线程安全

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值