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. Spring事务管理2----编程式事务管理

    编程式事务管理 通过使用将Spring框架提供的TransactionTemplate模板注入到业务层来进行事务管理,这样对业务层原来的代码修改过多.不利于项目的后期维护. 以下是声明式事务管理的具体 ...

  2. 在线word转html

    http://www.docpe.com/word/word-to-html.aspx

  3. iOS-保存图片到相册

    //保存      UIButton *saveBtn = [[UIButton alloc] init];    //    saveBtn.frame = CGRectMake((screenWi ...

  4. ubuntu 16 搭建只能上传不可下载删除ftp服务

    安装 VSFTPD,(建议使用FileZill测试,报错能看到原因) 如果使用window文件管理连接,要注意下图的设置 使用 apt-get 安装 vsftpd sudo apt-get insta ...

  5. RabbitMQ简单实现,exchange四种模式,持久化

    RabbitMQ目录 一.简介,简单实现二.Exchange四种类型简单介绍三.消息确认,交换机.队列及消息持久化一.简介及简单实现RabbitMQ是一个消息代理:它接受并转发消息.你可以把它当成一个 ...

  6. 微信小程序 左右分类滚动列表

    今天需求个类似得到app分类的功能,效果如图: 左右分别滚动,互不干扰,先把简单的布局和样式搭好. <view class="page"> <view class ...

  7. Vue Cli 3 初体验(全面详解)

    vue新出了 vue cli 3,并直接改名为 @vue/cli,今天就来盘他. 首先介绍等啰里啰嗦的就不写了,贴个link吧. Vue CLi3 github Vue CLi web 要是想先了解下 ...

  8. ffmpeg学习笔记-初识ffmpeg

    ffmpeg用来对音视频进行处理,那么在使用ffmpeg前就需要ffmpeg有一个大概的了解,这里使用雷神的ppt素材进行整理,以便于复习 音视频基础知识 视频播放器的原理 播放视频的流程大致如下: ...

  9. rqnoj PID95:多多看DVD(加强版)

    题目描述 多多进幼儿园了,今天报名了.只有今晚可以好好放松一下了(以后上了学后会很忙).她的叔叔决定给他买一些动画片DVD晚上看.可是爷爷规定他们只能在一定的时间段L看完.(因为叔叔还要搞NOIP不能 ...

  10. 《MIT 6.828 Lab1: Booting a PC》实验报告

    <MIT 6.828 Lab1: Booting a PC>实验报告 本实验的网站链接见:Lab 1: Booting a PC. 实验内容 熟悉x86汇编语言.QEMU x86仿真器.P ...