Collection of SE-10 -- collection tool class

Keywords: Java

11. Collections

Collection of utility classes, including many practical methods

1. Binary search of the list: the collection must be ordered.

int binarySearch(list,key);

//The list must be upgraded and sorted according to the natural order of the elements

//It is required that the elements in the list collection are all subclasses of compatible.

int binarySearch(list,key,Comparator);

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

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(741);
        list.add(456);
        list.add(123);
        list.add(19);
        Collections.sort(list); // Keep the set in order
        System.out.println(Collections.binarySearch(list, 19)); // 1
        System.out.println(Collections.binarySearch(list, 741)); // 4
        System.out.println(Collections.binarySearch(list, 0)); // -1
    }
}

2. Sort the list set.

sort(list);

//To sort the list, use the compareTo method of objects in the list container

sort(list,comaprator);

//Sort according to the comparison rules of the specified comparer

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

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(741);
        list.add(456);
        list.add(123);
        list.add(19);
        System.out.println(list); // [5, 741, 456, 123, 19]
        Collections.sort(list); // Ascending sort
        System.out.println(list); // [5, 19, 123, 456, 741]
    }
}

3. Take the maximum or minimum value for the set.

max(Collection)

max(Collection,comparator)

min(Collection)

min(Collection,comparator)

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

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(741);
        list.add(456);
        list.add(123);
        list.add(19);
        System.out.println(Collections.max(list)); // 741
    }
}

4. Reverse the list set.

reverse(list);

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

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(741);
        list.add(456);
        list.add(123);
        list.add(19);
        System.out.println(list); // [5, 741, 456, 123, 19]
        Collections.reverse(list); // Flip
        System.out.println(list); // [19, 123, 456, 741, 5]
    }
}

5. Reverse the way of comparison.

Comparator reverseOrder();

Comparator reverseOrder(Comparator);

6. Replace the elements in the list collection.

swap(list,x,y);

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

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(741);
        list.add(456);
        list.add(123);
        list.add(19);
        System.out.println(list); // [5, 741, 456, 123, 19]
        Collections.swap(list, 2, 4);
        System.out.println(list); // [5, 741, 19, 123, 456]
    }
}

7. Replace the elements of the list collection.

If the element being replaced does not exist, the original collection does not change.

replaceAll(list,old,new);

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

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(741);
        list.add(456);
        list.add(123);
        list.add(19);
        System.out.println(list); // [5, 741, 456, 123, 19]
        Collections.replaceAll(list, 5, 6);
        System.out.println(list); // [6, 741, 19, 123, 456]
        Collections.replaceAll(list, 7, 8);
        System.out.println(list); // [6, 741, 19, 123, 456]
    }
}

8. You can change an asynchronous set into a synchronized set.

Set synchronizedSet(Set<T> set)

Map synchronizedMap(Map<K,V> map)

List synchronizedList(List<T> list)

Posted by phpnoobie on Thu, 30 Apr 2020 01:05:00 -0700