Collection 和 Collections的区别
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的区别的更多相关文章
- Collection 和 Collections的区别。
Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...
- Java学习笔记--Collection和Collections的区别
转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别. 1.java.util.Collection 是一个集合 ...
- Collection 和 Collections的区别。(转)
Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...
- 介绍Collection框架的结构;Collection 和 Collections的区别
介绍Collection框架的结构:Collection 和 Collections的区别 集合框架: Collection:List列表,Set集 Map:Hashtable,HashMap,Tre ...
- HashMap和Hashtable的区别--List,Set,Map等接口是否都继承自Map接口--Collection和Collections的区别
面试题: 1.HashMap和Hashtable的区别? HashMap:线程不安全,效率高,键和值都允许null值 Hashtable:线程安全,效率低,键和值都不允许null值 ArrayList ...
- Collection 和 Collections的区别?
Collection 和 Collections的区别? 解答:Collection是java.util下的接口,它是各种集合的父接口,继承于它的接口主要有Set 和List:Collections是 ...
- Java集合【6】-- Collection和Collections的区别
刚开始学java的时候,分不清Collection和Collections,其实这两个东西是完全不一样的东西. Collection是一个接口,是java集合中的顶级接口之一,衍生出了java集合的庞 ...
- Collection和Collections的区别?
Collection 是接口(Interface),是集合类的上层接口. Collections是类(Class),集合操作的工具类,服务于Collection框架.它是一个算法类,提供一系列静态方法 ...
- java中Collection和Collections的区别
1.Collection: 它是java集合类的一个通用接口,所有集合类都实现的它 2.Collections: 它是一个封装集合类常用工具方法的类,不能被示例化,只支持静态调用
随机推荐
- faster-rcnn 测试自己数据集训练的模型
python demo_2019051601.pyTraceback (most recent call last): File "demo_2019051601.py", lin ...
- windows和Linux下定时启动或关闭服务
http://blog.csdn.net/clare504/article/details/17410643 1.Windows下的定时启动程序可以采用系统的计划和任务,定时启动服务可以在计划任务中添 ...
- 编译bitcoin比特币客户端
我遇到了两个不太容易解决的问题. 问题一: checking for Berkeley DB C++ headers... default configure: error: Found Berkel ...
- web框架初阶
第一站 文件结构:web--- |--home.py #页面处理函数 |--index.py #主体函数 |--indexPlus.py #主体函数加强版 |--webdaem.py #通过we ...
- Re0:在.NetCore 中Dapper的基本用法
整理一下目前在用的Dapper 与FrameWork不同,NetCore数据库配置需要从appsettings.json中获取 刚接触的时候被这块坑了,自己手动建了个app.config.然后你懂的( ...
- IDEA debug漏洞第二弹(fastjson,version<1.2.47)
首先这个漏洞调试不需要非要使用docker,本身是一个jar包的问题.所以我们可以自己写一个小java代码来直接调试. POC如下 {"name":{"@type&quo ...
- matlab imread函数全说明
imread Read image from graphics file Syntax A = imread(filename, fmt) [X, map] = imread(...) [...] ...
- luogu P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles (递推)
链接:https://www.luogu.org/problemnew/show/P1216 题面: 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的 ...
- storm常见问题
一.storm ui 不显示 supervisor 信息 在zk节点上删掉storm信息,重新启动storm集群即可
- HttpServletRequest对象(转)
HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供 ...