赞
踩
sort(List<T> list)
对 List 进行排序:List<Integer> numbers = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6));
Collections.sort(numbers);
System.out.println("排序后的列表:" + numbers);
reverse(List<?> list)
反转 List 中的元素:List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie", "David"));
Collections.reverse(names);
System.out.println("反转后的列表:" + names);
shuffle(List<?> list)
随机打乱 List 中的元素:List<Character> letters = new ArrayList<>(Arrays.asList('A', 'B', 'C', 'D', 'E'));
Collections.shuffle(letters);
System.out.println("打乱后的列表:" + letters);
binarySearch(List<? extends Comparable<? super T>> list, T key)
在已排序的 List 中查找元素:List<Integer> sortedNumbers = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9));
int index = Collections.binarySearch(sortedNumbers, 5);
System.out.println("元素 5 的索引位置是:" + index);
addAll(Collection<? super T> c, T... elements)
将指定的元素全部添加到集合中:List<String> colors = new ArrayList<>();
Collections.addAll(colors, "Red", "Green", "Blue");
System.out.println("添加元素后的列表:" + colors);
frequency(Collection<?> c, Object o)
返回指定元素在集合中出现的次数:List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 4, 2, 5));
int frequency = Collections.frequency(numbers, 2);
System.out.println("元素 2 在列表中出现的次数:" + frequency);
fill(List<? super T> list, T obj)
使用指定元素替换 List 中的所有元素:List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie", "David"));
Collections.fill(names, "John");
System.out.println("替换后的列表:" + names);
min(Collection<? extends T> coll)
返回集合中的最小元素:List<Integer> numbers = new ArrayList<>(Arrays.asList(9, 6, 2, 8, 4));
Integer min = Collections.min(numbers);
System.out.println("最小元素是:" + min);
max(Collection<? extends T> coll)
返回集合中的最大元素:List<Integer> numbers = new ArrayList<>(Arrays.asList(9, 6, 2, 8, 4));
Integer max = Collections.max(numbers);
System.out.println("最大元素是:" + max);
copy(List<? super T> dest, List<? extends T> src)
将源 List 中的元素复制到目标 List 中:List<String> source = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry"));
List<String> destination = new ArrayList<>(Arrays.asList("Mango", "Grapes", "Orange"));
Collections.copy(destination, source);
System.out.println("目标列表:" + destination);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。