Java集合框架 工具类Collections
Collections是一个类,容器的工具类,就如同Arrays是数组的工具类
步骤 1 : 反转
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 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);
}
}
步骤 2 : 混淆
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 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);
}
}
步骤 3 : 排序
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 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);
}
}
步骤 4 : 交换
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 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);
}
}
步骤 5 : 滚动
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 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);
}
}
步骤 6 : 线程安全化
synchronizedList 把非线程安全的List转换为线程安全的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 numbers = new ArrayList<>();
System.out.println("把非线程安全的List转换为线程安全的List");
List synchronizedNumbers = (List) Collections.synchronizedList(numbers);
}
}
练习: 统计概率
首先初始化一个List,长度是10,值是0-9。
然后不断的shuffle,直到前3位出现
3 1 4
shuffle 1000,000 次,统计出现的概率
答案 :
package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
List 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+"%");
}
}