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. 等保测评中与oracle有关的工作

    等保2.0包含硬件.存储.中间件.数据库各方面的安全规范,现把与Oracle数据库有关的内容整理如下,供参考: 一.安全计算环境 1.身份鉴别: a,应对登陆的用户进行身份标识和鉴别,身份标识具有唯一 ...

  2. Pandas-数据处理-基础部分

    有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/11014882.html  jupyter 代码 ...

  3. echarts的一点记录

    echart官网地址: https://www.echartsjs.com/index.html echarts实例地址:https://echarts.baidu.com/examples/ vue ...

  4. 前端自己导出Excel

    1.导出当前页面,这是最简单的,只是导出当前页面的数据. exportData(tableid, name) { let table; let uri = 'data:application/vnd. ...

  5. KVM虚拟化——简介

    KVM 基于内核的虚拟机KVM(Kernel-Based Virtual Machine)是2007年问世的开源虚拟化解决方案.KVM需要两个条件: ①硬件支持全虚拟化 ②操作系统为Linux KVM ...

  6. Spring Boot配置多数据源并实现Druid自动切换

    原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...

  7. Goodbye Microservices: From 100s of problem children to 1 superstar

    https://segment.com/blog/goodbye-microservices/ Unless you’ve been living under a rock, you probably ...

  8. Vue界面中关于APP端回调方法问题

    在混合开发中,HTML界面经常性的需要调用APP端提供的原生方法,而且在很多时候,APP端需要各种回调,如果将所有的回调方法写在内部,不是很方便,而且有些时候,APP端需要定义一些主动触发HTML界面 ...

  9. 使用Anaconda管理Python环境

    修改镜像源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda con ...

  10. 05_Tutorial 5: Relationships & Hyperlinked APIs 关系和超链接

    1.关系和超链接 0.文档 https://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/ h ...