Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作。

1) 排序(Sort)使用sort方法可以根据元素的自然顺序 对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable接口。此列表内的所有元素都必须是使用指定比较器可相互比较的
double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.sort(list);
for (int i = 0; i < array.length; i++) {
  System.out.println(li.get(i));
}
//结果:112,111,23,456,231
2) 混排(Shuffling)
混排算法所做的正好与 sort 相反: 它打乱在一个 List 中可能有的任何排列的踪迹。也就是说,基于随机源的输入重排该 List,这样的排列具有相同的可能性(假设随机源是公正的)。这个算法在实现一个碰运气的游戏中是非常有用的。例如,它可被用来混排代表一副牌的Card 对象的一个 List 。另外,在生成测试案例时,它也是十分有用的。
Collections.Shuffling(list)
double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.shuffle(list);
for (int i = 0; i < array.length; i++) {
  System.out.println(li.get(i));
}
//结果:112,111,23,456,231
3) 反转(Reverse)
      使用Reverse方法可以根据元素的自然顺序 对指定列表按降序进行排序。
Collections.reverse(list)
double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections. reverse (list);
for (int i = 0; i < array.length; i++) {
  System.out.println(li.get(i));
}
//结果:231,456,23,111,112
4) 替换所有的元素(Fill)
使用指定元素替换指定列表中的所有元素。
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<str.length;j++){
li.add(new String(str[j]));
}
Collections.fill(li,"aaa");
for (int i = 0; i < li.size(); i++) {
System.out.println("list[" + i + "]=" + li.get(i));

}
//结果:aaa,aaa,aaa,aaa,aaa

5) 拷贝(Copy)
用两个参数,一个目标 List 和一个源 List, 将源的元素拷贝到目标,并覆盖它的内容。目标 List至少与源一样长。如果它更长,则在目标 List 中的剩余元素不受影响。
Collections.copy(list,li): 后面一个参数是目标列表 ,前一个是源列表
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {1131,333};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
Collections.copy(list,li);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果:1131,333,23,456,231
6) 返回Collections中最小元素(min)
根据指定比较器产生的顺序,返回给定 collection 的最小元素。collection中的所有元素都必须是通过指定比较器可相互比较的
Collections.min(list)
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.min(list);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果:23
7) 返回Collections中最小元素(max)
根据指定比较器产生的顺序,返回给定 collection 的最大元素。collection中的所有元素都必须是通过指定比较器可相互比较的
Collections.max(list)
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.max(list);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果:456
8) lastIndexOfSubList
返回指定源列表中最后一次出现指定目标列表的起始位置
int count = Collections.lastIndexOfSubList(list,li);
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
Int locations = Collections. lastIndexOfSubList (list,li);
System.out.println(“===”+ locations);
//结果 3
9) IndexOfSubList
返回指定源列表中第一次出现指定目标列表的起始位置
int count = Collections.indexOfSubList(list,li);
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
Int locations = Collections.indexOfSubList(list,li);
System.out.println(“===”+ locations);
//结果 1
10) Rotate
根据指定的距离循环移动指定列表中的元素
Collections.rotate(list,-1);
如果是负数,则正向移动,正数则方向移动
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.rotate(list,-1);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果:111,23,456,231,112

(转)Collections类方法详解的更多相关文章

  1. JAva Collections类方法详解

    http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...

  2. Collections类方法详解

    Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序(Sort)使用sort方法可以根据元素的自然顺序 对 ...

  3. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  4. 第7.15节 Python中classmethod定义的类方法详解

    第7.15节  Python中classmethod定义的类方法详解 类中的方法,除了实例方法外,还有两种方法,分别是类方法和静态方法.本节介绍类方法的定义和使用. 一.    类方法的定义 在类中定 ...

  5. IOS NSBundle 的理解和 mainBundle 类方法详解

    常看到类似的 NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil]; 这样的代码,用来获取 file 的完全 ...

  6. JAVA中Object类方法详解

    一.引言 Object是java所有类的基类,是整个类继承结构的顶端,也是最抽象的一个类.大家天天都在使用toString().equals().hashCode().waite().notify() ...

  7. Collections.synchronizedMap 详解

    众所周知,HashMap 本身非线程安全的,但是当使用 Collections.synchronizedMap(new HashMap()) 进行包装后就返回一个线程安全的Map. 怎么实现的呢?今天 ...

  8. Routemanage类方法详解

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Ref ...

  9. python collections模块详解

    参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...

随机推荐

  1. 测序分析软件-trimmomatic的记录

    1.下载相关软件,网址:http://www.usadellab.org/cms/index.php?page=trimmomatic,它有源代码和二进制两种文件(建议都下载,然后合并成一个文件,因为 ...

  2. 条件随机场CRF(一)从随机场到线性链条件随机场

    条件随机场CRF(一)从随机场到线性链条件随机场 条件随机场CRF(二) 前向后向算法评估观察序列概率(TODO) 条件随机场CRF(三) 模型学习与维特比算法解码(TODO) 条件随机场(Condi ...

  3. ListView实现下拉刷新和上拉加载功能

    1 public class RefreshListView extends ListView implements OnScrollListener { private View mHeaderVi ...

  4. flask笔记一

    最近学习flask,由于web开发方面接触的并不是很多,所以看官方文档有点焦头烂额,好多的概念不理解. <Flask web 开发>比较基础,先用这本书做个入门. 1.Flask实例化对象 ...

  5. 【RAC】RAC相关基础知识

    [RAC]RAC相关基础知识 1.CRS简介    从Oracle 10G开始,oracle引进一套完整的集群管理解决方案—-Cluster-Ready Services,它包括集群连通性.消息和锁. ...

  6. 大话Python中*args和**kargs的使用

    对于初学者来说,看到*args和**kargs就头大,到底它们有何用处,怎么使用?这篇文章将为你揭开可变参数的神秘面纱 1.*args 实质就是将函数传入的参数,存储在元组类型的变量args当中 de ...

  7. 自己动手写一个自动登录脚本gg

    1.下载一个sshpass工具 2.安装sshpass,安装到tools文件夹 3.把tools文件夹的路径加入到/etc/bashrc vim   /etc/bashrc 最后一行  : expor ...

  8. php追加数组

    <?php //追加数组 array_merge_recursive()函数与array_merge()相同,可以将两个或多个数组合并在一起,形成一个联合的数组.两者之间的区别在于,当某个输入数 ...

  9. Iconfont 矢量图标库的应用

    前言: 在项目开发中,不免在标签栏,工具栏等应用各种各样的小图标. 然后老旧的做法就是要UI设计出各种图标并生成图片给到我们,但是这样就存在了一个问题,每次请求页面的时候就需要发送请求请求图片,这样不 ...

  10. shell中source与sh区别

    shell中使用source conf.sh,是直接运行conf.sh的命令,不创建子shell,类似与html中include,而sh是则创建子shell, 子shell里面 的变量父shell无法 ...