Collections类常用方法总结
1. sort
对集合进行排序
public static <T extends Comparable<? super T>> void sort(List<T> list) public static <T> void sort(List<T> list,
Comparator<? super T> c)
在使用List时想根据List中存储对象的某一字段进行排序,那么我们要用到Collections.sort方法对list排序,用Collections.sort方法对list排序有两种方法:
- 第一种是list中的对象实现Comparable接口;
- 第二种方法是根据Collections.sort重载方法来实现。
示例如下:
public class SortTest {
public static void main(String[] args) {
List<String> listS = new ArrayList<String>();
List<Employer1> list1 = new ArrayList<Employer1>();
List<Employer2> list2 = new ArrayList<Employer2>();
List<Employer3> list3 = new ArrayList<Employer3>();
//一.将String类型的变量插入到listS中并排序
//listS中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序
listS.add("5");
listS.add("2");
listS.add("9");
Collections.sort(listS);
//二.将Employer1类的对象插入到list1中并排序
//将已创建的实现了Comparator接口的比较类MyCompare传入Collections的sort方法中即可实现依照MyCompare类中的比较规则。
Employer1 a1 = new Employer1();
Employer1 b1 = new Employer1();
Employer1 c1 = new Employer1();
a1.setName("a1"); a1.setAge(44);
b1.setName("b1"); b1.setAge(55);
c1.setName("b1"); c1.setAge(33);
list1.add(a1);
list1.add(b1);
list1.add(c1);//Collections类的sort方法要求传入的第二个参数是一个已实现Comparator接口的比较器
Collections.sort(list1, new MyCompare());
//三.将Employer2类的对象插入到list2中并排序
//其实原理和上面的二类似,只是没有单独创建MyCompare类,而是用匿名内部类来实现Comparator接口里面的具体比较。
Employer2 a2 = new Employer2();
Employer2 b2 = new Employer2();
Employer2 c2 = new Employer2();
a2.setName("a2"); a2.setAge(66);
b2.setName("b2"); b2.setAge(33);
c2.setName("b2"); c2.setAge(22);
list2.add(a2);
list2.add(b2);
list2.add(c2); //Collections类的sort方法要求传入的第二个参数是一个已实现Comparator接口的比较器
Collections.sort(list2,new Comparator<Employer2>(){
@Override
public int compare(Employer2 a2, Employer2 b2) {
return a2.getOrder().compareTo(b2.getOrder());
}
});
//四.将Employer3类的对象插入到list3中并排序
//被排序的类Employer3实现了Comparable接口,在类Employer3中通过重载compareTo方法来实现具体的比较。
Employer3 a3 = new Employer3();
Employer3 b3 = new Employer3();
Employer3 c3 = new Employer3();
a3.setName("a3"); a3.setAge(77);
b3.setName("b3"); b3.setAge(55);
c3.setName("b3"); c3.setAge(99);
list3.add(a3);
list3.add(b3);
list3.add(c3);
Collections.sort(list3);//Collections类的sort方法要求传入的List中的对象是已实现Comparable接口的对象
System.out.println(listS);
System.out.println(list1);
System.out.println(list3);
System.out.println(list2);
}
}
class Employer1{
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override//重载了Object类里的toString方法,使之可以按照我们要求的格式打印
public String toString() {
return "name is "+name+" age is "+ age;
}
}
class MyCompare implements Comparator<Employer1> {
@Override//重载了Comparator接口里面的compare方法实现具体的比较
public int compare(Employer1 o1, Employer1 o2) {
return o1.getAge().compareTo(o2.getAge());
}
}
class Employer2{
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override//重载了Object类里的toString方法,使之可以按照我们要求的格式打印
public String toString() {
return "name is "+name+" age is "+age;
}
}
class Employer3 implements Comparable<Employer3>{
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override//重载了Object类里的toString方法,使之可以按照我们要求的格式打印
public String toString() {
return "name is "+name+" age is "+age;
}
@Override//重载了Comparable接口里的compareTo方法来实现具体的比较
public int compareTo(Employer3 a) {
return this.age.compareTo(a.getAge());
}
}
打印的结果为:
[2, 5, 9]
[name is b1 age is 33, name is a1 age is 44, name is b1 age is 55]
[name is b3 age is 55, name is a3 age is 77, name is b3 age is 99]
[name is b2 age is 22, name is b2 age is 33, name is a2 age is 66]
★compareTo()小结
由上面的程序我们可以看出,无论是实现了Comparable接口的方法还是实现了Comparator接口的方法,最终比较的返回值都是通过compareTo方法实现的,故就把compareTo方法单独拿出来做个小结。
compareTo()的返回值是整型,它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度。例如:
String s1 = "abc";
String s2 = "abcd";
String s3 = "abcdfg";
String s4 = "1bcdfg";
String s5 = "cdfg";
System.out.println( s1.compareTo(s2) ); // -1 (前面相等,s1长度小1)
System.out.println( s1.compareTo(s3) ); // -3 (前面相等,s1长度小3)
System.out.println( s1.compareTo(s4) ); // 48 ("a"的ASCII码是97,"1"的的ASCII码是49,所以返回48)
System.out.println( s1.compareTo(s5) ); // -2 ("a"的ASCII码是97,"c"的ASCII码是99,所以返回-2)
2. shuffle
对集合进行随机排序
public static void shuffle(List<?> list) public static void shuffle(List<?> list, Random rnd)
示例:
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("w");
c.add("o");
c.add("r");
c.add("l");
c.add("d");
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
}
}
运行结果为:
[w, o, r, l, d]
[l, d, w, o, r]
[o, r, d, l, w]
3. binarySearch
查找指定集合中的元素,返回所查找元素的索引
public static <T> int binarySearch(List<? extends Comparable<? super T>> list,
T key) public static <T> int binarySearch(List<? extends T> list,
T key,
Comparator<? super T> c)
示例:
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("w");
c.add("o");
c.add("r");
c.add("l");
c.add("d");
System.out.println(c);
int m = Collections.binarySearch(c, "o");
System.out.println(m);
}
}
运行结果为:[w, o, r, l, d]
注意:若查找的元素不存在,示例中的n即表示该元素最有可能存在的位置的索引。
4. max
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) public static <T> T max(Collection<? extends T> coll,
Comparator<? super T> comp)
前者采用Collection内含自然比较法,后者采用Comparator进行比较.
5. min
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) public static <T> T min(Collection<? extends T> coll,
Comparator<? super T> comp)
前者采用Collection内含自然比较法,后者采用Comparator进行比较。
6. indexOfSubList
查找subList在list中首次出现位置的索引
public static int indexOfSubList(List<?> source,
List<?> target)
示例:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
List subList = Arrays.asList("three four five six".split(" "));
System.out.println(Collections.indexOfSubList(list, subList));
}
}
运行结果为:[one, two, three, four, five, six, siven]
7. lastIndexOfSubList
使用与上例方法的使用相同,在此就不做介绍了。
8. replaceAll
替换批定元素为某元素,若要替换的值存在刚返回true,反之返回false
public static <T> boolean replaceAll(List<T> list,
T oldVal,
T newVal)
示例:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
List subList = Arrays.asList("three four five six".split(" "));
System.out.println(Collections.replaceAll(list, "siven", "siven eight"));
System.out.println(list);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
9. reverse()
反转集合中元素的顺序
public static void reverse(List<?> list)
示例:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
10. rotate
集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来
public static void rotate(List<?> list,
int distance)
示例:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
Collections.rotate(list, 1);
System.out.println(list);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[siven, one, two, three, four, five, six]
11. copy
将集合n中的元素全部复制到m中,并且覆盖相应索引的元素
public static <T> void copy(List<? super T> dest,
List<? extends T> src)
示例:
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
List n = Arrays.asList("我 是 复制过来的哈".split(" "));
System.out.println(n);
Collections.copy(m,n);
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[我, 是, 复制过来的哈]
[我, 是, 复制过来的哈, four, five, six, siven]
12. swap
交换集合中指定元素索引的位置
public static void swap(List<?> list,
int i,
int j)
示例:
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.swap(m, 2, 3);
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
13. fill
用对象o替换集合list中的所有元素
public static <T> void fill(List<? super T> list,
T obj)
示例:
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.fill(m, "haha52T25xixi");
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi]
14. nCopies
返回大小为n的List,List不可改变,其中的所有引用都指向o
public static <T> List<T> nCopies(int n,
T o)
示例:
public class Practice {
public static void main(String[] args){
System.out.println(Collections.nCopies(5, "haha"));
}
}
运行结果为:
[haha, haha, haha, haha, haha]
参考:http://www.360doc.com/content/14/0829/10/15242507_405537400.shtml
Collections类常用方法总结的更多相关文章
- 【Java集合的详细研究1】Collections类常用方法总结
1.sort(Collection)方法的使用(含义:对集合进行排序). 例:对已知集合c进行排序? public class Practice { public static void main(S ...
- Java从入门到放弃18---Map集合/HashMap/LinkedHashMap/TreeMap/集合嵌套/Collections工具类常用方法
Java从入门到放弃18—Map集合/HashMap/LinkedHashMap/TreeMap/集合嵌套/Collections工具类常用方法01 Map集合Map集合处理键值映射关系的数据为了方便 ...
- Java 集合的工具类Collections的常用方法
Collections类 java.utils.Collections是集合工具类,用来对集合进行操作. Collections类的常用方法 这里介绍四个常用方法: addAll(Collection ...
- Collections 类和Arrays类常用方法详解
1:Collections类 max(Collection <? extends T> coll):根据元素的自然顺序,返回给定集合元素中的最大元素 min(Collection < ...
- Colletions工具类常用方法
Collections 工具类常用方法: 排序 查找,替换操作 同步控制(不推荐,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合 排序操作 void reverse(List list)/ ...
- Arrays类与Collections类
java.util.Arrays类包含一个静态的工厂,允许数组被视为列表.以下是关于数组的要点: 这个类包含了各种方法来操作数组(比如排序和搜索). 在这个类中的方法抛出NullPointerExce ...
- Java中的Collections类
转载:https://blog.csdn.net/yangxingpa/article/details/80515963 从[Java]Java中的Collections类——Java中升级版的数据结 ...
- Java API —— Collections类
1.Collections类概述 针对集合操作 的工具类,都是静态方法 2.Collections成员方法 public static <T> void ...
- Java基础知识强化之集合框架笔记68:Collections类概述和成员方法(备注:Collection 和 Collections区别)
1. Collections类概述: 针对集合操作的工具类,都是静态方法. 2. Collection 和 Collections的区别: Collections是个java.util下的类,它包含 ...
随机推荐
- XE6移动开发环境搭建之IOS篇(4):VMware9里安装Mac OSX 10.8(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 以下内容比较长,我们 ...
- 清理Oracle安装目录里的一些日志信息
最近测试环境服务器上磁盘空间紧张,表空间里面的数据也不知道开发哪些需要哪些不需要,而且因为此测试库运行时间比较久远,起码有三年了.于是就从清理Oracle安装目录下的日志下手. 发现在一般这几个日志, ...
- JAVA设计模式之迭代子模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述迭代子(Iterator)模式的: 迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不 ...
- 用wireshark抓包分析TCP三次握手、四次挥手以及TCP实现可靠传输的机制
关于TCP三次握手和四次挥手大家都在<计算机网络>课程里学过,还记得当时高超老师耐心地讲解.大学里我遇到的最好的老师大概就是这位了,虽然他只给我讲过<java程序设计>和< ...
- 【caffe-windows】 caffe-master 之 matlab接口配置
平台环境: win10 64位 caffe-master vs2013 Matlab2016a 第一步: 打开\caffe-master\windows下的CommonSettings.props文 ...
- 如何利用tomcat和cas实现单点登录(1):配置tomcat的ssl和部署cas
如何利用tomcat和cas实现单点登录,借鉴了网上的很多教程,主要分为以下几个步骤: 一:下载好cas,tomcat之后,首先配置tomcat: 用鼠标右键点击"计算机"→选择& ...
- sql 分组查询及格不及格人数
select score as 类别,count(*) as 人数 from (select case when fen>=60 then '及格' else '不及格' end as scor ...
- 【C#基础】System.Reflection (反射)
在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于"自成一体"的单元中,这个单元称为装配件.我们可以在程序运行期间访问这些信息.在System. ...
- Android IOS WebRTC 音视频开发总结(七三)-- 我为什么走上了创业这条不归路?
本文主要介绍自己为什么选择创业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 2016.06.01对公司来说是个很 ...
- LeetCode344:Reverse String@Python
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...