Collections集合工具類
包位置: java.util.Collections
常用方法:(省略前面的public)
- static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素
- static void shuffle(List<?> list) : 打亂順序粤铭,打亂集合順序
- static <T> void sort(List<T> list):將集合中元素按照默認規(guī)則排序。
- static <T> void sort(List<T> list, Comparator<? super T>):將集合中元素按照指定規(guī)則排序售滤。
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list, 1,2,3,4,5,6,7,8,9,0);
System.out.println("原始: " + list);
Collections.shuffle(list);
System.out.println("打亂: " + list);
Collections.sort(list);
System.out.println("默認排序: " + list);
Collections.sort(list, (a, b) -> b - a);
System.out.println("規(guī)則排序:" + list);