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. 5G 与 MEC 边缘计算

    目录 文章目录 目录 前言 参考文献 通信网络 核心网演进之路 早古时期 2G 网络架构 3G 网络架构 4G 网络架构 5G 5G 网络的需求 5G 网络架构的设计原则 5G 网络的逻辑架构 5G ...

  2. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_05-vuejs研究-vuejs基础-v-text指令

    把js移到body 的下面 网速改慢一点 通过模拟网速慢的情况.刷新页面的时候会有闪烁的效果 速度快的情况下也会闪烁 ,只不过是不明显. 2.解决插值表达式闪烁问题,使用v-text v-text可以 ...

  3. SQL Server 高级函数汇总【转】

    看到一个帖子,博主收集的很全,里面涵盖了一些常用的内置函数,特此收藏下: 原文链接:https://blog.csdn.net/wang1127248268/article/details/53406 ...

  4. 【ML】seq2seq原理

    最近要做分享,重温seq2seq原理,这篇文章讲的挺清楚: https://zhuanlan.zhihu.com/p/40920384 https://www.jianshu.com/p/b2b95f ...

  5. dp[2019.5.25]

    1.实例计算(写出计算过程): 1)对维数为序列<5, 10, 3, 12, 5, 50, 6>的各矩阵,找出其矩阵链乘积的一个最优加全部括号. 这是一个矩阵连乘问题,基本知识可以参考: ...

  6. Flutter打包release版本安卓apk包真机安装无法请求网络的解决方法

    今天flutter build apk打包了一个release.apk包,在真机上安装后网络数据都不显示,但是在模拟器上没问题,然后又连接真机开debug各种测试,一切都正常!那这会是什么问题呢? 查 ...

  7. 【并行计算与CUDA开发】英伟达硬件加速编解码

    硬件加速 并行计算 OpenCL OpenCL API VS SDK 英伟达硬件编解码方案 基于 OpenCL 的 API 自己写一个编解码器 使用 SDK 中的编解码接口 使用编码器对于 OpenC ...

  8. NDK学习笔记-NDK开发流程

    本文主要是说明一下在eclipse下如何对NDK进行配置 配置NDK 虽然现在基本上都使用Android Studio进行Android开发,但一些项目在eclipse中仍有运用,这里讲一讲eclip ...

  9. Node async 控制代码执行顺序

    当你有一个集合,你想循环集合,然后对每个集合按照顺序执行相应的方法你可以使用forEachSeries

  10. [CF429E]Points ans Segments_欧拉回路

    Points and Segments 题目链接:www.codeforces.com/contest/429/problem/E 注释:略. 题解: 先离散化. 发现每个位置如果被偶数条线段覆盖的话 ...