Collections工具类介绍
- Collections是一个操作Set、 List和Map等集合的工具类
- Collections中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作
排序操作:(均为static方法)
- reverse(List): 反转List中元素的顺序
- shuffle(List): 对List集合元素进行随机排序
- sort(List): 根据元素的自然顺序对指定List集合元素按升序排序
也可以sort(Listm Comparator)按指定比较器产生的顺序来排序
- swap(List, int, int): 将指定list集合中的i处元素与j处元素交换
查找、替换
- Object max(Collection) :根据元素自然顺序,返回给定集合中最大元素
- Object max(Collection, Comparator)
- Object min
- int frequency(Collection, Object):返回集合中指定元素的出现次数
- void copy(List dest, List src): src中内容复制到dest
- boolean replaceAll(List list, Object oldVal, Object newVal): 使用新值替换List对象的所有旧值
本文章使用limfx的vscode插件快速发布