Java self - Collections Collections Framework

Java Collections Framework Tools Collections

Collections class is a container class of tool, like an array of tools Arrays

Step 1: reverse

reverse the data from the List of flip occurs
Reverse

package collection;
   
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
   
public class TestCollection {
    public static void main(String[] args) {
        //初始化集合numbers
        List<Integer> numbers = new ArrayList<>();
         
        for (int i = 0; i < 10; i++) {
            numbers.add(i);
        }
         
        System.out.println("集合中的数据:");
        System.out.println(numbers);
         
        Collections.reverse(numbers);
         
        System.out.println("翻转后集合中的数据:");
        System.out.println(numbers);
         
    }
}

Step 2: confusion

shuffle confusion order of data in List
Confusion

package collection;
   
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
   
public class TestCollection {
    public static void main(String[] args) {
        //初始化集合numbers
        List<Integer> numbers = new ArrayList<>();
         
        for (int i = 0; i < 10; i++) {
            numbers.add(i);
        }
         
        System.out.println("集合中的数据:");
        System.out.println(numbers);
         
        Collections.shuffle(numbers);
         
        System.out.println("混淆后集合中的数据:");
        System.out.println(numbers);
         
    }
}

Step 3: Sort

sort of data to sort List
Sequence

package collection;
   
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
   
public class TestCollection {
    public static void main(String[] args) {
        //初始化集合numbers
        List<Integer> numbers = new ArrayList<>();
         
        for (int i = 0; i < 10; i++) {
            numbers.add(i);
        }
         
        System.out.println("集合中的数据:");
        System.out.println(numbers);
 
        Collections.shuffle(numbers);
        System.out.println("混淆后集合中的数据:");
        System.out.println(numbers);
 
        Collections.sort(numbers);
        System.out.println("排序后集合中的数据:");
        System.out.println(numbers);
         
    }
}

Step 4: exchange

swap exchanging location data of two
exchange

package collection;
   
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
   
public class TestCollection {
    public static void main(String[] args) {
        //初始化集合numbers
        List<Integer> numbers = new ArrayList<>();
         
        for (int i = 0; i < 10; i++) {
            numbers.add(i);
        }
         
        System.out.println("集合中的数据:");
        System.out.println(numbers);
 
        Collections.swap(numbers,0,5);
        System.out.println("交换0和5下标的数据后,集合中的数据:");
        System.out.println(numbers);
         
    }
}

Step 5: Scroll

rotate the data in the List, scroll right to specify units of length
scroll

package collection;
   
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
   
public class TestCollection {
    public static void main(String[] args) {
        //初始化集合numbers
        List<Integer> numbers = new ArrayList<>();
         
        for (int i = 0; i < 10; i++) {
            numbers.add(i);
        }
         
        System.out.println("集合中的数据:");
        System.out.println(numbers);
 
        Collections.rotate(numbers,2);
        System.out.println("把集合向右滚动2个单位,标的数据后,集合中的数据:");
        System.out.println(numbers);
         
    }
}

Step 6: thread safety of

synchronizedList the non-thread-safe List converted to thread-safe List.

package collection;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class TestCollection {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
 
        System.out.println("把非线程安全的List转换为线程安全的List");
        List<Integer> synchronizedNumbers = (List<Integer>) Collections.synchronizedList(numbers);
 
    }
}

Exercise : statistical probability

First initialize a List, the length is 10, the value is 0-9.
Then continue to shuffle, until the three appeared
314

shuffle 1000,000 times, probability and statistics appear

Answer:

package collection;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class TestCollection {
    public static void main(String[] args) {
        List<Integer> ls = new ArrayList<>();
 
        for (int i = 0; i < 10; i++) {
            ls.add(i);
        }
        int count = 0;
         
        for (int i = 0; i < 1000 * 1000; i++) {
            Collections.shuffle(ls);
            if(ls.get(0)==3 && ls.get(1)==1 && ls.get(2)==4)
                count++;
        }
        double rate = count/(1000d*1000);
        System.out.println("出现的概率是"+rate*100+"%");
 
    }
 
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/12034366.html