总体方法一览
sort
这个方法可以实现List集合的排序
有两个重载形式
-
public static <T extends Comparable<? super T>> void sort(List<T> list)
默认的排序方法,是升序排序
-
public static <T> void sort(List<T> list, Comparator<? super T> c)
该方法可以自定义排序规则
默认排序方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| ArrayList<Integer> arrayList = new ArrayList<>();
Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(random.nextInt(100)); }
System.out.println(arrayList);
Collections.sort(arrayList);
System.out.println(arrayList);
|
自定义排序方法
需要传入Comparator对象,并且重写compare方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| ArrayList<Integer> arrayList = new ArrayList<>();
Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(random.nextInt(100)); }
System.out.println(arrayList);
Collections.sort(arrayList, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } });
System.out.println(arrayList);
|
shuffle
对List中的元素进行随机排序(洗牌)
该方法有两个重载
-
public static void shuffle(List<?> list)
使用默认的随机算法随机排序
-
public static void shuffle(List<?> list, Random rnd)
指定随机算法随机排序
但是因为是随机的结果,所以我觉得指定随机算法可有可无,本人一切从简
默认随机算法
1 2 3 4 5 6 7 8 9 10 11 12
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(random.nextInt(100)); } System.out.println(arrayList);
Collections.shuffle(arrayList); System.out.println(arrayList);
|
指定随机算法
1 2 3 4 5 6 7 8 9 10 11
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(random.nextInt(100)); } System.out.println(arrayList);
Collections.shuffle(arrayList, new Random(5)); System.out.println(arrayList);
|
reverse
逆转List集合中的元素顺序
1 2 3 4 5 6 7 8 9 10 11 12
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
Collections.reverse(arrayList); System.out.println(arrayList);
|
swap
交换List集合i和j位置的元素
public static void swap(List<?> list, int i, int j)
1 2 3 4 5 6 7 8 9 10 11 12
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
Collections.swap(arrayList, 0, 9); System.out.println(arrayList);
|
max
返回Collection集合中最大的元素
该方法有两种重载
-
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
使用默认比较规则
-
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
使用指定比较规则
默认比较规则
1 2 3 4 5 6 7 8 9 10 11
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
System.out.println("max = " + Collections.max(arrayList));
|
指定比较规则
比如我们可以调用max方法返回Collection集合中最小的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
System.out.println("max = " + Collections.max(arrayList, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }));
|
min
返回Collection集合中最小的元素
该方法有两种重载
-
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
使用默认比较规则
-
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
使用指定比较规则
默认比较规则
1 2 3 4 5 6 7 8 9 10 11
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
System.out.println("min = " + Collections.min(arrayList));
|
指定比较规则
比如我们可以调用min方法返回Collection集合中最大的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
System.out.println("min = " + Collections.min(arrayList, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }));
|
copy
将src的所有元素拷贝到dest中,这个操作会覆盖dest之前的元素
public static <T> void copy(List<? super T> dest, List<? extends T> src)
执行这个命令的前提是dest.size() >= src.size()
否则会抛出异常IndexOutOfBoundsException(“Source does not fit in dest”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ArrayList<Integer> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList); ArrayList<Integer> distList = new ArrayList<>(); for (int i = 0; i < 20; i++) { distList.add(99); } System.out.println(distList);
Collections.copy(distList, arrayList); System.out.println(distList);
|
replaceAll
将List集合中的所有oldValue换成newValue
1 2 3 4 5 6 7 8 9 10 11 12
| ArrayList<String> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add("ikun"); } System.out.println(arrayList);
Collections.replaceAll(arrayList, "ikun", "鸡你太美"); System.out.println(arrayList);
|
frequency
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
返回某个元素在Collection集合中出现的次数
1 2 3 4 5 6 7 8 9 10
| ArrayList<String> arrayList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { arrayList.add("ikun"); } System.out.println(arrayList);
System.out.println("ikun frequency = " + Collections.frequency(arrayList,"ikun"));
|
addAll
将一个数组的所有元素全部添加到Collection集合中去
1 2 3 4 5 6 7
| ArrayList<Integer> arrayList = new ArrayList<>(); Integer[] arrays = new Integer[]{1, 2, 3, 4 ,5 ,6 ,7 ,8 , 9, 10};
Collections.addAll(arrayList,arrays); System.out.println(arrayList);
|
fill
将Collection集合的所有元素填充以固定的值
1 2 3 4 5 6 7 8 9 10 11
| ArrayList<Integer> arrayList = new ArrayList<>(10); for (int i = 0; i < 10; i++) { arrayList.add(i); } System.out.println(arrayList);
Collections.fill(arrayList, 10); System.out.println(arrayList);
|