首页 技术 正文
技术 2022年11月10日
0 收藏 328 点赞 5,032 浏览 3130 个字

Java集合框架 工具类Collections

Collections是一个类,容器的工具类,就如同Arrays是数组的工具类

步骤 1 : 反转

reverse 使List中的数据发生翻转

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); }
}

步骤 2 : 混淆

shuffle 混淆List中数据的顺序

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); }
}

步骤 3 : 排序

sort 对List中的数据进行排序

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); }
}

步骤 4 : 交换

swap 交换两个数据的位置

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); }
}

步骤 5 : 滚动

rotate 把List中的数据,向右滚动指定单位的长度

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); }
}

步骤 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<Integer> numbers = new ArrayList<>(); System.out.println("把非线程安全的List转换为线程安全的List");
List<Integer> synchronizedNumbers = (List<Integer>) 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<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+"%"); }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285