Java 的 Collections 类

2023-12-14 11:42:32

Java 的 Collections 类

java.util.Collections 类提供了许多静态方法来操作集合(Collections)。以下是其中一些主要的方法,并附有相应的示例:

  1. sort(List<T> list) / sort(List<T> list, Comparator<? super T> c)

    // 对列表进行排序(自然顺序)
    List<Integer> numbers = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5));
    Collections.sort(numbers);
    System.out.println(numbers); // 输出: [1, 1, 2, 3, 4, 5, 5, 6, 9]
    
    // 使用自定义Comparator对列表进行排序
    List<String> words = new ArrayList<>(Arrays.asList("apple", "orange", "banana", "grape"));
    Collections.sort(words, Comparator.comparing(String::length));
    System.out.println(words); // 输出: [grape, apple, orange, banana]
    
  2. binarySearch(List<? extends Comparable<? super T>> list, T key) / binarySearch(List<? extends T> list, T key, Comparator<? super T> c)

    // 在已排序的列表中进行二分查找
    List<Integer> sortedNumbers = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9));
    int index = Collections.binarySearch(sortedNumbers, 5);
    System.out.println("Index of 5: " + index); // 输出: Index of 5: 2
    
    // 在自定义排序后的列表中进行二分查找
    List<String> sortedWords = new ArrayList<>(Arrays.asList("apple", "orange", "banana", "grape"));
    Collections.sort(sortedWords, Comparator.comparing(String::length));
    int indexCustom = Collections.binarySearch(sortedWords, "banana", Comparator.comparing(String::length));
    System.out.println("Index of 'banana': " + indexCustom); // 输出: Index of 'banana': 2
    
  3. reverse(List<?> list)

    // 反转列表中的元素顺序
    List<Character> characters = new ArrayList<>(Arrays.asList('a', 'b', 'c', 'd', 'e'));
    Collections.reverse(characters);
    System.out.println(characters); // 输出: [e, d, c, b, a]
    
  4. shuffle(List<?> list)

    // 打乱列表中的元素顺序
    List<Integer> shuffledNumbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
    Collections.shuffle(shuffledNumbers);
    System.out.println(shuffledNumbers); // 输出: [3, 1, 5, 2, 4](随机顺序)
    
  5. fill(List<? super T> list, T obj)

    // 将列表中的所有元素替换为指定的对象
    List<String> strings = new ArrayList<>(Arrays.asList("apple", "orange", "banana"));
    Collections.fill(strings, "grape");
    System.out.println(strings); // 输出: [grape, grape, grape]
    
  6. copy(List<? super T> dest, List<? extends T> src)

    // 将一个列表的元素复制到另一个列表
    List<Integer> source = new ArrayList<>(Arrays.asList(1, 2, 3));
    List<Integer> destination = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0));
    Collections.copy(destination, source);
    System.out.println(destination); // 输出: [1, 2, 3, 0, 0]
    
  7. addAll(Collection<? super T> c, T... elements)

    // 将指定元素添加到集合
    List<String> fruits = new ArrayList<>(Arrays.asList("apple", "orange"));
    Collections.addAll(fruits, "banana", "grape");
    System.out.println(fruits); // 输出: [apple, orange, banana, grape]
    
  8. frequency(Collection<?> c, Object o)

    // 获取集合中指定元素的出现次数
    List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 1, 2, 1, 3, 4));
    int frequencyOfOne = Collections.frequency(numbers, 1);
    System.out.println("Frequency of 1: " + frequencyOfOne); // 输出: Frequency of 1: 3
    

这些方法使得对集合进行排序、搜索、反转、填充等常见操作变得更加简单和方便。

文章来源:https://blog.csdn.net/Go_ahead_forever/article/details/134849884
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。