Common methods of Java tools Collections

Sort operation

Collections provides the following methods to sort List

  • void reverse(List list): reverse

  • void shuffle(List list) random sort

  • void sort(List list) Sort in ascending order of natural sorting, storage elements need to implement the Comparable interface, and override the compareTo method

  • void sort(List list, Comparator c) Customized sorting, the sorting logic is controlled by Comparator

  • void swap(List list, int i, int j) swap the elements at two index positions

  • void rotate(List list, int distance), rotate. When distance is a positive number, move the distance elements after the list to the front as a whole. When distance is negative, move the first distance elements of the list to the back as a whole.

Simple example:

package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsTest {
    
    
    public static void main(String[] args) {
    
    
        ArrayList nums =  new ArrayList();
        nums.add(8);
        nums.add(-3);
        nums.add(2);
        nums.add(9);
        nums.add(-2);
        System.out.println(nums);
        Collections.reverse(nums);
        System.out.println(nums);
        Collections.sort(nums);
        System.out.println(nums);
        Collections.shuffle(nums);
        System.out.println(nums);
        //下面只是为了演示定制排序的用法,将int类型转成string进行比较
        Collections.sort(nums, new Comparator() {
    
    

            @Override
            public int compare(Object o1, Object o2) {
    
    
                // TODO Auto-generated method stub
                String s1 = String.valueOf(o1);
                String s2 = String.valueOf(o2);
                return s1.compareTo(s2);
            }

        });
        System.out.println(nums);
    }
}

result:

1 [8, -3, 2, 9, -2]
2 [-2, 9, 2, -3, 8]
3 [-3, -2, 2, 8, 9]
4 [9, -2, 8, 2, -3]
5 [-2, -3, 2, 8, 9]

Find, replace operation

  • int binarySearch(List list, Object key), perform binary search on List, return index, note that List must be in order

  • int max(Collection coll), returns the largest element according to the natural order of the elements;

Analogy int min(Collection coll).

  • int max(Collection coll, Comparator c), returns the largest element according to custom sorting, and the sorting rules are controlled by the Comparatator class;

Analogy int min(Collection coll, Comparator c).

  • void fill(List list, Object obj), fill all elements in list with element obj

  • int frequency(Collection c, Object o), count the number of occurrences of elements

  • int indexOfSubList(List list, List target), counts the index of the first occurrence of Targe in the list, returns -1 if it is not found, analogous to int lastIndexOfSubList(List source, list target).

  • boolean replaceAll(List list, Object oldVal, Object newVal), replace the old element with the new element.

Simple example:

package collection.collections;

import java.util.ArrayList;
import java.util.Collections;

public class CollectionsTest {
    
    
    public static void main(String[] args) {
    
    
        ArrayList num =  new ArrayList();
        num.add(3);
        num.add(-1);
        num.add(-5);
        num.add(10);
        System.out.println(num);
        System.out.println(Collections.max(num));
        System.out.println(Collections.min(num));
        Collections.replaceAll(num, -1, -7);
        System.out.println(Collections.frequency(num, 3));
        Collections.sort(num);
        System.out.println(Collections.binarySearch(num, -5));
    }
}

result:

1 [3, -1, -5, 10]
2 10
3 -5
4 1
5 1

Guess you like

Origin blog.csdn.net/qq_43229056/article/details/108937659