Object-oriented language (JAVA) - tool class (Collections (operation collection) code implementation understanding)

The tool class, as the name implies, is the class of tools in Java.

The Collections class is a tool class provided by Java to operate collections such as Set, List and Map. The Collections class provides many static methods for manipulating collections. With these static methods, operations such as sorting, searching, replacing, and copying of collection elements can be realized.

 Collections provides the following methods for sorting the elements of the List collection.


1. void reverse(List list): Reverse sorts the elements of the specified List collection .

2. void shuffle(List list): Randomly sort the elements of the List collection (the shuffle method simulates the " shuffling " action).

3. void sort(List list): Sort the elements of the specified List collection in ascending order according to the natural order of the elements .

4. void sort(List list, Comparator c): Sort the List collection elements according to the order generated by the specified Comparator.

5. void swap(List list, int i, int j): exchange the element at i and the element at j in the specified List collection.

6. void rotate(List list, int distance): When the distance is a positive number, move the last distance elements of the list collection to the front; when the distance is a negative number, move the first distance elements of the list collection to the front "Move to the back. This method does not change the length of the collection.

int binarySearch(List list, Object key): Use the binary search method to search the specified List collection to obtain the index of the specified object in the List collection. If you want this method to work properly, you must ensure that the elements in the List are already in an ordered state.

Object max(Collection coll): Returns the largest element in the given collection according to the natural order of the elements.

Object max(Collection coll, Comparator comp): Returns the largest element in the given collection, in the order specified by Comparator.

Object min(Collection coll): Returns the smallest element in the given collection according to the natural order of the elements.

Object min(Collection coll, Comparator comp): Returns the smallest element in the given collection, in the order specified by Comparator.

void fill(List list, Object obj): Replace all elements in the specified List collection with the specified element obj.

int frequency(Collection c, Object o): Returns the number of occurrences of the specified element in the specified collection.

int indexOfSubList(List source, List target): Returns the position index of the first occurrence of the sub-List object in the parent List object; if such a sub-List does not appear in the parent List, returns -1.

int lastIndexOfSubList(List source, List target): Returns the index of the last occurrence of the sub-List object in the parent List object; if such a sub-List does not appear in the parent List, returns -1.

boolean replaceAll(List list, Object oldVal, Object newVal): Replaces all old values ​​oldVal of the List object with a new value newVal. 

In Collections.copy(list, Copylist), overwrite the content of the former with the content of the latter 

When outputting the data in the List, if you just want to display the data, use

 System.out.println(list);

If you still need to operate on a certain one, use an iterator

Iterator iterator1 = list.iterator();
while (iterator1.hasNext())
        {
            System.out.print(iterator1.next()+" ");
        }

public class CollectionsTest
{
    public static void main(String[] args)
    {
        List list = new ArrayList();
        List Copylist = new ArrayList();
        list.add(4);
        list.add(3);
        list.add(6);
        list.add(1);
        list.add(9);
        System.out.println("—————排序从低到高——————");
        Collections.sort(list);//排序从低到高
        System.out.println(list);

        System.out.println("—————排序从高到低——————");
        Collections.reverse(list);
        Iterator iterator1 = list.iterator();
        while (iterator1.hasNext())
        {
            System.out.print(iterator1.next()+" ");
        }

        System.out.println("\n"+"—————随机排序——————");
        Collections.shuffle(list);
        System.out.println(list);

        System.out.println("\n"+"—————重置内容——————");
        Collections.fill(list,"Lungcen");
        System.out.println(list);

        System.out.println("\n"+"—————覆盖内容——————");
        Copylist.add("H");
        Copylist.add("HH");
        Copylist.add("HHH");
        Collections.copy(list,Copylist);
        System.out.println(list);
    }
}

Guess you like

Origin blog.csdn.net/qq_64552181/article/details/127837075