Java基础知识之集合工具类Arrays和Collections

一.Arrays类:
常用方法(将数组转换为一个集合):
public static List asList(T… a) 等价于public static List asList(T[] a).
在这里插入图片描述
在这里插入图片描述
注意: 通过Arrays.asList方法得到的List对象的长度是固定的,不能增,也不能减.
asList方法返回的ArrayList对象,不是java.util.ArrayList而是Arrays类中的内部类对象.

在这里插入图片描述
在这里插入图片描述
二.Collections类
Collections类:封装了Set,List,Map的操作的工具方法.
获取空集对象(没有元素的集合,注意集合不为null):
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

常用的集合类:
HashSet/ArrayList/HashMap都是线程不安全的,在多线程环境下不安全.
在Collections类中有获取线程安全的集合方法:
List list = Collections.synchronizedList(new ArrayList());
当要做迭代的时候得使用synchronized.
synchronized(list) {
TODO
}

Set set = Collections.synchronizedSet(new HashSet());

Map map = Collections.synchronizedMap(new HashMap());
在这里插入图片描述
三.
在这里插入图片描述
四.在这里插入图片描述

发布了99 篇原创文章 · 获赞 2 · 访问量 2598

猜你喜欢

转载自blog.csdn.net/weixin_41588751/article/details/105313143