Collections (tool class for collection processing)

Collections (tool class for collection processing)

1. What is Collections?

This class is composed only by static methods or returns collections. It contains polymorphic algorithms, "wrappers", that operate on collections, returning a new collection backed by the specified collection.

2. The main methods are as follows:

  • addAll(Collection<? super T> c, T… elements) Add all specified elements to the specified collection.
  • binarySearch(List<? extends Comparable<? super T>> list, T key) Searches the specified list for the specified object using the binary search algorithm.
  • copy(List<? super T> dest, List<? extends T> src) Copies all elements from one list to another.
  • emptyList() returns an empty list (immutable).
  • fill(List<? super T> list, T obj) replaces all elements of the specified list with the specified elements.
  • max(Collection<? extends T> coll) Returns the maximum element of the given collection according to the natural order of its elements.
  • min(Collection<? extends T> coll) Returns the smallest element of the given collection according to the natural order of its elements.
  • replaceAll(List list, T oldVal, T newVal) Replaces all occurrences of one specified value in the list with another.
  • reverse(List<?> list) Reverses the order of the elements in the specified list.
  • sort(List list, Comparator<? super T> c) Sorts the specified list according to the order induced by the specified comparator.
  • swap(List<?> list, int i, int j) swaps the elements at the specified positions in the specified list.

3. Code implementation.

package collectionsDemo;

import java.util.*;

public class CollectionsDemo {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<String> alist = new ArrayList<>();
        alist.add("a");
        alist.add("b");
        alist.add("c");
        Collections.addAll(alist,"d","e","f");//addAll(Collection<? super T> c, T... elements)将所有指定的元素添加到指定的集合。
        System.out.println(alist);
        ArrayList<Integer> alist1 = new ArrayList<>();
        alist1.add(1);
        alist1.add(3);
        alist1.add(2);
        alist1.add(7);
        alist1.add(4);
        alist1.add(2);
//        Collections.sort(alist1);
//        Collections.sort(alist1, new Comparator<Integer>() {//可自定义排序
//            @Override
//            public int compare(Integer o1, Integer o2) {
    
    
//                return o1-o2;
//            }
//        });
        Collections.sort(alist1,((o1, o2) -> {
    
    return o2-o1;}));//Lambda表达式
        System.out.println(alist1);
        System.out.println(Collections.binarySearch(alist1,1));//binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
        // 使用二叉搜索算法搜索指定对象的指定列表。
        Collections.swap(alist1,0,2);//交换指定列表中指定位置的元素
        System.out.println(alist1);

        ArrayList<Integer> alist2 = new ArrayList<>();
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        alist2.add(0);
        Collections.copy(alist2,alist1);//copy(List<? super T> dest, List<? extends T> src)
        // 将所有元素从一个列表复制到另一个列表中。
        System.out.println(alist2);

        System.out.println(Collections.max(alist1));
        System.out.println(Collections.min(alist1));

        Collections.replaceAll(alist2,0,1);//将旧集合中的某一元素替换为新元素
        System.out.println(alist2);

        Collections.reverse(alist2);//翻转集合中元素
        System.out.println(alist2);

        List<Integer> alist3 = new ArrayList<>();
        alist3=Collections.emptyList();//返回一个不能更改的集合;

        System.out.println(alist3);
    }
}

4. Running results

[a, b, c, d, e, f]
[7, 4, 3, 2, 2, 1]
-1
[3, 4, 7, 2, 2, 1]
[3, 4, 7, 2, 2, 1, 0, 0, 0]
7
1
[3, 4, 7, 2, 2, 1, 1, 1, 1]
[1, 1, 1, 1, 2, 2, 7, 4, 3]
[]

Guess you like

Origin blog.csdn.net/weixin_52340450/article/details/122375579