1、java.util.Collection

  是一个集合接口(集合类的一个顶级接口)。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式,其直接继承接口有List与Set和Queue。

2、java.util.Collections

  是一个包装类(工具类/帮助类)。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,用于对集合中元素进行排序、搜索以及线程安全等各种操作,服务于Java的Collection框架。

常用的有:

(1)排序 sort(Collection)

如Collections.sort(List<T> list),Collections.sort(List<T> list, Comparator<? super T> c)。

使用sort方法可以根据元素的自然顺序对指定列表按升序进行排序。列表中的所有元素都必须实现Comparable接口, 而且必须是使用指定比较器可相互比较的。

 @SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
 @SuppressWarnings({"unchecked", "rawtypes"})
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}

简单示例:

 public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.sort(list);
System.out.println(list);
}
}

运行结果:

[five, four, one, three, two]   //(按字母排序)

(2)混排 shuffle(Collection)

如Collections.shuffle(List<?> list)

基于随机源的输入随机排序该List,这个算法对实现一个碰运气的游戏非常有用,在生成测试案例时也十分有用。

简单示例:

 public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.shuffle(list);
System.out.println(list);
}
}

运行结果:

[three, five, four, one, two]

(3)反转 reverse(Collection)

如Collections.reverse(List<?> list)
使用reverse()反转集合中元素的顺序

简单示例:

 public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.reverse(list);
System.out.println(list);
}
}

运行结果:

[five, four, three, two, one]

(4)替换所有元素 fill(List list,Object o)

使用指定元素替换集合中的所有元素。

简单示例:

 public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.fill(list, "zero");
System.out.println(list);
}
}

运行结果:

[zero, zero, zero, zero, zero]

(5)拷贝 copy(List list1,List list2)

将集合list2中的元素全部复制到list1中,并且覆盖相应索引的元素。目标list1至少与源list2一样长。

简单示例:

 public class Test {
public static void main(String[] args) {
List list1 = Arrays.asList("one two three four five".split(" "));
List list2 = Arrays.asList("一 二 三 四 五".split(" "));
Collections.copy(list1, list2);
System.out.println(list1);
}
}

运行结果

[一, 二, 三, 四, 五]

(6)rotate(List list,int m)

根据指定的距离m循环移动列表中的元素。集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来。

简单示例:

 public class Test {
public static void main(String[] args) {
List list1 = Arrays.asList("one two three four five".split(" "));
Collections.rotate(list1, 2);
System.out.println(list1);
}
}

运行结果:

[four, five, one, two, three]

(7)最小(大)元素 min(),max()

根据指定比较器产生的顺序,返回给定Collection的最小(大)元素。

min(Collection),min(Collection,Comparator)
max(Collection),max(Collection,Comparator)

简单示例:

 public class Test {
public static void main(String[] args) {
List list = new ArrayList();
list.add(10);
list.add(40);
list.add(20);
list.add(50);
System.out.println(Collections.min(list));
System.out.println(Collections.max(list));
}
}

运行结果:

10
50

(8)indexOfSublist(List list,List sublist)

查找sublist在list中首次出现位置的索引。返回指定源列表中第一次出现指定目标列表的起始位置。

简单示例:

 public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
List subList=Arrays.asList("three four five".split(" "));
System.out.println(Collections.indexOfSubList(list,subList)); }
}

运行结果:

2

(9)lastIndexOfSublist(List list,List sublist)

返回指定列表中最后一次出现指定目标列表的起始位置。

(10)swap(List list,int m,int n)

交换集合中指定元素索引m,n的位置。

 public class Test {
public static void main(String[] args) {
List list = Arrays.asList("one two three four five".split(" "));
Collections.swap(list, 2, 3);
System.out.println(list);
}
}

运行结果:

[one, two, four, three, five]

参考:

1、https://www.jianshu.com/p/0494cce4312a

2、https://www.cnblogs.com/kadaj174/p/11021730.html

 

Collection 和 Collections的区别的更多相关文章

  1. Collection 和 Collections的区别。

    Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...

  2. Java学习笔记--Collection和Collections的区别

    转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别.   1.java.util.Collection 是一个集合 ...

  3. Collection 和 Collections的区别。(转)

    Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...

  4. 介绍Collection框架的结构;Collection 和 Collections的区别

    介绍Collection框架的结构:Collection 和 Collections的区别 集合框架: Collection:List列表,Set集 Map:Hashtable,HashMap,Tre ...

  5. HashMap和Hashtable的区别--List,Set,Map等接口是否都继承自Map接口--Collection和Collections的区别

    面试题: 1.HashMap和Hashtable的区别? HashMap:线程不安全,效率高,键和值都允许null值 Hashtable:线程安全,效率低,键和值都不允许null值 ArrayList ...

  6. Collection 和 Collections的区别?

    Collection 和 Collections的区别? 解答:Collection是java.util下的接口,它是各种集合的父接口,继承于它的接口主要有Set 和List:Collections是 ...

  7. Java集合【6】-- Collection和Collections的区别

    刚开始学java的时候,分不清Collection和Collections,其实这两个东西是完全不一样的东西. Collection是一个接口,是java集合中的顶级接口之一,衍生出了java集合的庞 ...

  8. Collection和Collections的区别?

    Collection 是接口(Interface),是集合类的上层接口. Collections是类(Class),集合操作的工具类,服务于Collection框架.它是一个算法类,提供一系列静态方法 ...

  9. java中Collection和Collections的区别

    1.Collection: 它是java集合类的一个通用接口,所有集合类都实现的它 2.Collections: 它是一个封装集合类常用工具方法的类,不能被示例化,只支持静态调用

随机推荐

  1. 用VLC读取摄像头产生RTSP流,DSS主动取流转发(一)

    用VLC读取摄像头产生RTSP流,DSS主动取流转发(一) 摄像机地址是192.1.101.51,VLC运行在192.1.101.77上,DSS服务器架设在192.1.101.30上. Step1:V ...

  2. 配置Xmanager远程登录

    1.修改/etc/gdm/custom.conf文件找到下面的语句:[security]AllowRemoteRoot=true[xdmcp]Enable=true修改custom.conf后,必须重 ...

  3. swift 第十一课 结构体定义model类

    结构体是可以作为 model 类使用的不过也要 写下的创建方法 import UIKit/***创建一个model 结构,重写init 方法,结构体的属性不能出现可选类型**/ struct Mode ...

  4. Python随笔日记(1)

    Python学习 1.安装python .之后在Windows中配置环境变量(计算机\属性\高级系统设置\环境变量\系统变量\path后加入 :路径) 2.注意变量的命名的规则 字母.数字.下划线 p ...

  5. bootstrap table checkbox获得选中得数据

    var idlist = $('#table').bootstrapTable('getAllSelections');     for (var i = 0; i < idlist.lengt ...

  6. GDB获取帮助信息

    用help 功能,你可以获得GDB 的命令信息. helph 你可以用help(缩写h)不带参数来显示一个命令分类的简短列表. (gdb) help List of classes of comman ...

  7. 【gcd】辗转相除法

    #include<stdio.h> int gcd(int a, int b) { int c; while(b) { c = a % b; a = b; b = c; } return ...

  8. 学习笔记:CentOS7学习之二十一: 条件测试语句和if流程控制语句的使用

    目录 学习笔记:CentOS7学习之二十一: 条件测试语句和if流程控制语句的使用 21.1 read命令键盘读取变量的值 21.1.1 read常用见用法及参数 21.2 流程控制语句if 21.2 ...

  9. java核心技术(第四章)对象与类

    4.1 面向对象程序设计概述 每个对象包含对用户公开的特定功能部分和隐藏的实现部分. 4.1.1类 由类构造对象的过程称为创建类的实例. 封装(数据隐藏)是与对象有关的.从形式上看,封装不过是将数据和 ...

  10. [转帖]Intel 上一代 可扩展CPU的简单报价

    8.1万元人间毒物!Intel 28核铂金版Xeon 8180零售上市 http://news.mydrivers.com/1/541/541670.htm 猜你想看:英特尔 CPU处理器 Xeon ...