Collections是一个包装类,其中包含有各种有关集合操作的静态多态方,比如可以作用在List和Set上,此类不能实例化。

排序
Integer[] array = new Integer[]{3, 10, 4, 0, 2};
List<Integer> integers = Arrays.asList(array);
Collections.sort(integers);
out.println(integers); //[0, 2, 3, 4, 10]

在这里借用了Arrays的asList方法,该方法可以将数组转成一个List。这里也只能对封装类型Integer数组调用asList方法,如果是int型的数组调用asList会出现其他问题。可以查考Arrays用法总结。

混排
out.println(integers); //[0, 2, 3, 4, 10]
Collections.shuffle(integers);
out.println(integers); //[0, 3, 10, 2, 4]

反转
out.println(integers); //[0, 3, 10, 2, 4]
Collections.reverse(integers);
out.println(integers); //[3, 0, 4, 2, 10]

填充(覆盖)元素
array = new Integer[]{3, 10, 4, 0, 2};
integers = Arrays.asList(array);
Collections.fill(integers, -1);
out.println(integers); //[-1, -1, -1, -1, -1]
out.println(Arrays.toString(array)); //[-1, -1, -1, -1, -1]

通过输出可以发现原数组的值也跟着发生了变化,因为是封装类型,所以Integer[]中和List<Integer>中的元素指向的是用一个Integer对象的地址,所以两个会一起变化。

替换元素
array = new Integer[]{3, 10, 4, 4, 4};
integers = Arrays.asList(array);
Collections.replaceAll(integers, 4, -4);
out.println(integers); //[3, 10, -4, -4, -4]

拷贝List
out.println(integers); //[3, 10, -4, -4, -4]
List<Integer> integersCopy = new ArrayList<>(); integersCopy.add(1);
integersCopy.add(1);
integersCopy.add(1);
integersCopy.add(1);
integersCopy.add(1);
integersCopy.add(1);
//如果不对List调用add方法,integerCopy还是空的,下面的copy方法会出错
Collections.copy(integersCopy, integers);
out.println(integersCopy); //[3, 10, -4, -4, -4, 1]

返回List中的最大值,最小值
array = new Integer[]{3, 10, 4, 0, 2};
integers = Arrays.asList(array);
out.println(integers);
out.println(Collections.min(integers)); //0
out.println(Collections.max(integers)); //10

源列表中第一次和最后一次出现指定列表的起始位置
Integer[] array1 = new Integer[]{3, 10, 4, 0, 2, 4, 0};
List integers1 = Arrays.asList(array1);
Integer[] array2 = new Integer[]{4, 0};
List integers2 = Arrays.asList(array2);
out.println(Collections.lastIndexOfSubList(integers1, integers2)); //5
out.println(Collections.indexOfSubList(integers1, integers2)); //2

循环移动
out.println(integers); [3, 10, 4, 0, 2]
Collections.rotate(integers, 1); //[2, 3, 10, 4, 0] 依次右移一位
out.println(integers);
Collections.rotate(integers, -2); //[10, 4, 0, 2, 3] 依次左移两位
out.println(integers);

二分查找,返回所在的下标。没有找到返回负的下标值。
array = new Integer[]{0, 2, 4, 10, 20};
integers = Arrays.asList(array);
out.println(Collections.binarySearch(integers, 4)); //2
out.println(Collections.binarySearch(integers, 9)); //-4

指定元素在集合中出现的次数
array = new Integer[]{0, 2, 4, 4, 20};
integers = Arrays.asList(array);
out.println(Collections.frequency(integers, 4)); //2

得到对象的n份拷贝
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
List<List<Integer>> lists = Collections.nCopies(3, numbers);
out.println(lists); //[[1], [1], [1]]
numbers.add(2);
out.println(lists); //[[1, 2], [1, 2], [1, 2]]

Collections.nCopies(n, object)返回一个List对象,该List对象中含有对象object的n份拷贝,然而实际上列表中并没有存储n份拷贝,从中任意取出一个时,得到的是同一个object。
---------------------
作者:DaydreamerZz
来源:CSDN
原文:https://blog.csdn.net/zhzh402/article/details/79672092

Collections用法总结的更多相关文章

  1. Java开发中的高频Collections用法总结与Java平台实现源代码查看方式

    一生二,二生三,三生万物,基础永远是一个计算机人的立身之本,相信看到这篇文章的人一般都知道数据结构这门课程,要不也不会找到我的这篇文章.数据结构这门课程的分析奠定了工程师对各种平台中的容器类,集合类的 ...

  2. JAVA - Collections用法总结

    一生二,二生三,三生万物,基础永远是一个计算机人的立身之本.数据结构这门课程的分析奠定了工程师对各种平台中的容器类,集合类的理解基础,正如好多人所说的,如果你对某个平台的集合类理解的不透彻,很可能,你 ...

  3. 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常

    1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...

  4. DataBinding 访问 3

    MVVM中的Model 我们可以用任何POJO 作为 data binding 的 Model, 但是直接修改POJO对象,不能直接更新UI android的 dataBinding 模块 给提供了通 ...

  5. System.Collections.Generic的各容器类的用法

    演示System.Collections.Generic的各容器类的用法. 包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSe ...

  6. Python collections 模块用法举例

    Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...

  7. Comparable和Comparator的区别&Collections.sort的两种用法

    在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...

  8. CSIC_716_20191118【常用模块的用法 Json、pickle、collections、openpyxl】

    序列化模块 序列化:将python或其他语言中的数据类型,转变成字符串类型. python中的八大数据类型回顾:int float str list tuple dict set bool 反序列化: ...

  9. collections中namedtuple的用法

    我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p = (1, 2) 但是,看到(1, 2),很难看出这个tuple是用来表示一个坐标的.这时,namedtuple就派上了用 ...

随机推荐

  1. C++实现企业链表(单向链表的另外一种实现方式)

    LinkList.h #include <windows.h> #include <stdio.h> // 链表小结点 typedef struct LINKNODE { LI ...

  2. MVC-区域(Area)

    1.启用路由前的准备工作 1.Global.asax.cs中注册路由 public class MvcApplication : System.Web.HttpApplication { protec ...

  3. Django 中使用权限认证

    权限认证 权限概念 """ 在实际开发中,项目中都有后台运营站点,运营站点里面会存在多个管理员, 那么不同的管理员会具备不同的任务和能力,那么要实现这样的管理员功能,那么 ...

  4. 版本控制系统(VCS)简介

    简介 版本控制系统(VCS)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.使用版本控制系统通常还意味着,就算你乱来一气把整个项目中的文件改的改删的删,你也照样可以轻松恢复到原先 ...

  5. 跨域访问支持(Spring Boot、Nginx、浏览器)

    原文:http://www.itmuch.com/work/cors/ 最近家中事多,好久没有写点啥了.一时间竟然不知从何说起.先说下最近家里发生的事情吧: 老爸肺气肿住院: 老妈甲状腺囊肿 儿子喘息 ...

  6. 【bzoj3238】差异 后缀树

    题目大意:给你一个字符串$S$,设$S_i$是串$S$第$i$长的后缀,求: $\sum\limits_{i=1}^{|S|} \sum\limits_{j=i+1}^{|S|} |S_i|+|S_j ...

  7. STM32的指令周期

    在keil中编程时,写了一行代码,然后就想知道,执行这句C代码需要多长时间. 时钟周期在这就不解释了,频率的倒数. 指令周期,个人理解就是cpu执行一条汇编指令所需要的时间. 我们知道cm3使用的三级 ...

  8. Windows10-Neo4j安装问题及解决方案

    暑假都过得差不多了才终于开始搭环境了 1.下载Neo4j Neo4j官网下载翻墙的话还可以 不翻墙的话下了好几次都下不下来 不用下载desktop,下载community server就可以了 2.下 ...

  9. Define Interfaces and Share Class Members through Mixins in Dart

    In this lesson, we will cover Interfaces and Mixins. Interfaces act as a contract containing propert ...

  10. 微信小程序 post 请求获取不到参数原因

    如果使用post 请求一定要加上 header: { "content-type": "application/x-www-form-urlencoded" } ...