collections类的使用

  • java.util.Collections类包含很多有用的方法,可以使程序员的工作变得更加容易,但是这些方法通常都没有被充分地利用。
  • 下面我就对常见方法做一个简单的概述,希望对大家能有所有帮助。
  • (1)排序
  • public static <T extends Comparable<? super T>> void sort(List<T> list)
  • 根据元素的自然顺序 对指定列表按升序进行排序。
  • public static void main(String[] args) {
  • List<Integer> list = new ArrayList<Integer>();
  • list.add(3);
  • list.add(1);
  • list.add(4);
  • list.add(5);
  • list.add(2);
  • System.out.println("排序前:"+list);
  • Collections.sort(list);
  • System.out.println("排序后:"+list);
  • }
  • 结果:
  • 排序前:[3, 1, 4, 5, 2]
  • 前序后:[1, 2, 3, 4, 5]
  • (2)反转
  • public static void reverse(List<?> list)
  • 反转指定列表中元素的顺序。
  • public static void main(String[] args) {
  • List<Integer> list = new ArrayList<Integer>();
  • list.add(3);
  • list.add(1);
  • list.add(4);
  • list.add(5);
  • list.add(2);
  • System.out.println("反转前:"+list);
  • Collections.reverse(list);
  • System.out.println("反转后:"+list);
  • }
  • 结果:
  • 反转前:[3, 1, 4, 5, 2]
  • 反转后:[2, 5, 4, 1, 3]
  • (3)交换
  • public static void swap(List<?> list,int i,int j)
  • 在指定列表的指定位置处交换元素。(如果指定位置相同,则调用此方法不会更改列表。)
  • public static void main(String[] args) {
  • List<Integer> list = new ArrayList<Integer>();
  • list.add(3);
  • list.add(1);
  • list.add(4);
  • list.add(5);
  • list.add(2);
  • System.out.println("交换前:"+list);
  • Collections.swap(list, 2, 4);
  • System.out.println("交换后:"+list);
  • }
  • 结果:
  • 交换前:[3, 1, 4, 5, 2]
  • 交换后:[3, 1, 2, 5, 4]
  • (4)获取最值
  • public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
  • 根据元素的自然顺序,返回给定 collection 的最大元素。
  • public static void main(String[] args) {
  • List<Integer> list = new ArrayList<Integer>();
  • list.add(3);
  • list.add(1);
  • list.add(4);
  • list.add(5);
  • list.add(2);
  • System.out.println("集合中的最大值是:"+Collections.max(list));
  • }
  • 结果:
  • 集合中的最大值是:5
  • 同理最小值
  • (5)public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key)
  • 使用二分搜索法搜索指定列表,以获得指定对象。
  • 注意:使用二分搜索法的时候,List集合得有前提,前提是该List是排序后的。
  • public static void main(String[] args) {
  • List<Integer> list = new ArrayList<Integer>();
  • list.add(1);
  • list.add(2);
  • list.add(3);
  • list.add(4);
  • list.add(5);
  • System.out.println("3在集合中的索引是:"+Collections.binarySearch(list,3));
  • }
  • 结果:
  • 3在集合中的索引是:2
  • (6)同步集合
  • public static <T> Collection<T> synchronizedCollection(Collection<T> c)返回指定 collection 支持的同步(线程安全的)collection。
  • public static <T> List<T> synchronizedList(List<T> list)返回指定列表支持的同步(线程安全的)列表。
  • public static <T> Set<T> synchronizedSet(Set<T> s)返回指定 set 支持的同步(线程安全的)set。
  • public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)返回由指定映射支持的同步(线程安全的)映射。
  • 获取同步集合的布置代码如下:
  • public static void main(String[] args) {
  • Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
  • List<String> list = Collections.synchronizedList(new ArrayList<String>());
  • Set<String> set = Collections.synchronizedSet(new HashSet<String>());
  • Map<String,String> m = Collections.synchronizedMap(new HashMap<String,String>());
  • }
  • 这样的话,我们就可以使用同步的集合类了。
  • (7)public static void shuffle(List<?> list)
  • 使用默认随机源对指定列表进行置换
  • 这样的话,我们每次的List输出的结果是不一致的。
  • 我们说一个应用场景,就是模拟发牌。(我们知道每副牌有52张,除去大小王,怎么着模拟发牌,每次发的牌都不一样呢?代码如下)
  • /*
  • * 模拟发牌的操作
  • */
  • public class CollectionsDemo2 {
  • // 定义扑克牌的所有花色和数值.
  • private String[] types = { "方块", "草花", "红心", "黑桃" };
  • private String[] values = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
  • "J", "Q", "K", "A" };
  • // 定义存储牌的集合
  • private List<String> list = new ArrayList<String>();
  • // 初始化牌
  • public void initCards() {
  • for (int i = 0; i < types.length; i++) {
  • for (int j = 0; j < values.length; j++) {
  • list.add(types + values[j]);
  • }
  • }
  • }
  • public static void main(String[] args) {
  • CollectionsDemo2 c = new CollectionsDemo2();
  • // 初始化牌
  • c.initCards();
  • // 洗牌
  • Collections.shuffle(c.list);
  • for (int x = 0; x < c.list.size(); x++) {
  • if (x % 13 == 0) {
  • System.out.println();
  • }
  • System.out.print(c.list.get(x) + " ");
  • }
  • }
  • }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值