1、Collections工具类
-
Collections下的方法都是静态方法,直接用类名访问
-
批量添加元素Collections.addAll(操作的集合对象,批量添加的元素值);
打乱list集合顺序Collections.shuffle(操作集合的对象);
给list集合默认升序排列Collections.sort(操作集合的对象); 对于字符串是根据首位符号编号升序排列
-
对引用类型的数据排序需要用到比较器对象,定制排序规则
2、Map集合
概述
-
Map集合是一种双列模式,每个元素含有两个值
-
每个元素的格式:key = value (键值对元素)
-
Map集合存储的信息更加丰富
-
Map集合体系:
Map<k , v> (接口,Map集合的祖宗类) ↓ HashMap<k , v> (实现类) ↓ LinkedHashMap<k , v> (实现类)
-
特点:
- Map集合的特点由键决定
- Map集合的键是无序,不重复,无索引的
- Map集合后面重复的键对应的元素会覆盖前面的整个元素
- Map集合的值无要求
- 键值对可以为null
- HashMap:元素按照键是无序,不重复,无索引,值无要求
- LinkedHashMap:元素按照键是有序,不重复,无索引,值无要求
-
public class MapDemo { public static void main(String[] args) { // 创建一个Map集合对象 Map<String , Integer> maps = new HashMap<>(); //Map<String , Integer> maps = new LinkedHashMap<>(); maps.put("AA",2); maps.put("BB" , 1); maps.put("CC" , 2); maps.put("DD" ,100 ); maps.put("EE" , 100 ); maps.put(null , null