Collections类常用方法总结
1. sort
对集合进行排序
1 public static <T extends Comparable<? super T>> void sort(List<T> list) 2 3 public static <T> void sort(List<T> list, 4 Comparator<? super T> c)
在使用List时想根据List中存储对象的某一字段进行排序,那么我们要用到Collections.sort方法对list排序,用Collections.sort方法对list排序有两种方法:
- 第一种是list中的对象实现Comparable接口;
- 第二种方法是根据Collections.sort重载方法来实现。
示例如下:
1 public class SortTest { 2 public static void main(String[] args) { 3 List<String> listS = new ArrayList<String>(); 4 List<Employer1> list1 = new ArrayList<Employer1>(); 5 List<Employer2> list2 = new ArrayList<Employer2>(); 6 List<Employer3> list3 = new ArrayList<Employer3>(); 7 8 //一.将String类型的变量插入到listS中并排序 9 //listS中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序 10 listS.add("5"); 11 listS.add("2"); 12 listS.add("9"); 13 Collections.sort(listS); 14 15 //二.将Employer1类的对象插入到list1中并排序 16 //将已创建的实现了Comparator接口的比较类MyCompare传入Collections的sort方法中即可实现依照MyCompare类中的比较规则。 17 Employer1 a1 = new Employer1(); 18 Employer1 b1 = new Employer1(); 19 Employer1 c1 = new Employer1(); 20 a1.setName("a1"); a1.setAge(44); 21 b1.setName("b1"); b1.setAge(55); 22 c1.setName("b1"); c1.setAge(33); 23 list1.add(a1); 24 list1.add(b1); 25 list1.add(c1);//Collections类的sort方法要求传入的第二个参数是一个已实现Comparator接口的比较器 26 Collections.sort(list1, new MyCompare()); 27 28 //三.将Employer2类的对象插入到list2中并排序 29 //其实原理和上面的二类似,只是没有单独创建MyCompare类,而是用匿名内部类来实现Comparator接口里面的具体比较。 30 Employer2 a2 = new Employer2(); 31 Employer2 b2 = new Employer2(); 32 Employer2 c2 = new Employer2(); 33 a2.setName("a2"); a2.setAge(66); 34 b2.setName("b2"); b2.setAge(33); 35 c2.setName("b2"); c2.setAge(22); 36 list2.add(a2); 37 list2.add(b2); 38 list2.add(c2); //Collections类的sort方法要求传入的第二个参数是一个已实现Comparator接口的比较器 39 Collections.sort(list2,new Comparator<Employer2>(){ 40 @Override 41 public int compare(Employer2 a2, Employer2 b2) { 42 return a2.getOrder().compareTo(b2.getOrder()); 43 } 44 45 }); 46 47 //四.将Employer3类的对象插入到list3中并排序 48 //被排序的类Employer3实现了Comparable接口,在类Employer3中通过重载compareTo方法来实现具体的比较。 49 Employer3 a3 = new Employer3(); 50 Employer3 b3 = new Employer3(); 51 Employer3 c3 = new Employer3(); 52 a3.setName("a3"); a3.setAge(77); 53 b3.setName("b3"); b3.setAge(55); 54 c3.setName("b3"); c3.setAge(99); 55 list3.add(a3); 56 list3.add(b3); 57 list3.add(c3); 58 Collections.sort(list3);//Collections类的sort方法要求传入的List中的对象是已实现Comparable接口的对象 59 60 System.out.println(listS); 61 System.out.println(list1); 62 System.out.println(list3); 63 System.out.println(list2); 64 } 65 } 66 class Employer1{ 67 private String name; 68 private Integer age; 69 public void setName(String name) { 70 this.name = name; 71 } 72 public Integer getAge() { 73 return age; 74 } 75 public void setAge(Integer age) { 76 this.age = age; 77 } 78 @Override//重载了Object类里的toString方法,使之可以按照我们要求的格式打印 79 public String toString() { 80 return "name is "+name+" age is "+ age; 81 } 82 } 83 class MyCompare implements Comparator<Employer1> { 84 @Override//重载了Comparator接口里面的compare方法实现具体的比较 85 public int compare(Employer1 o1, Employer1 o2) { 86 return o1.getAge().compareTo(o2.getAge()); 87 } 88 } 89 class Employer2{ 90 private String name; 91 private Integer age; 92 public void setName(String name) { 93 this.name = name; 94 } 95 public Integer getOrder() { 96 return age; 97 } 98 public void setAge(Integer age) { 99 this.age = age; 100 } 101 @Override//重载了Object类里的toString方法,使之可以按照我们要求的格式打印 102 public String toString() { 103 return "name is "+name+" age is "+age; 104 } 105 } 106 class Employer3 implements Comparable<Employer3>{ 107 private String name; 108 private Integer age; 109 public void setName(String name) { 110 this.name = name; 111 } 112 public Integer getAge() { 113 return age; 114 } 115 public void setAge(Integer age) { 116 this.age = age; 117 } 118 @Override//重载了Object类里的toString方法,使之可以按照我们要求的格式打印 119 public String toString() { 120 return "name is "+name+" age is "+age; 121 } 122 @Override//重载了Comparable接口里的compareTo方法来实现具体的比较 123 public int compareTo(Employer3 a) { 124 return this.age.compareTo(a.getAge()); 125 } 126 }
打印的结果为:
[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码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度。例如:
1 String s1 = "abc"; 2 String s2 = "abcd"; 3 String s3 = "abcdfg"; 4 String s4 = "1bcdfg"; 5 String s5 = "cdfg"; 6 System.out.println( s1.compareTo(s2) ); // -1 (前面相等,s1长度小1) 7 System.out.println( s1.compareTo(s3) ); // -3 (前面相等,s1长度小3) 8 System.out.println( s1.compareTo(s4) ); // 48 ("a"的ASCII码是97,"1"的的ASCII码是49,所以返回48) 9 System.out.println( s1.compareTo(s5) ); // -2 ("a"的ASCII码是97,"c"的ASCII码是99,所以返回-2)
2. shuffle
对集合进行随机排序
1 public static void shuffle(List<?> list) 2 3 public static void shuffle(List<?> list, Random rnd)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List c = new ArrayList(); 4 c.add("w"); 5 c.add("o"); 6 c.add("r"); 7 c.add("l"); 8 c.add("d"); 9 System.out.println(c); 10 Collections.shuffle(c); 11 System.out.println(c); 12 Collections.shuffle(c); 13 System.out.println(c); 14 } 15 }
运行结果为:
[w, o, r, l, d]
[l, d, w, o, r]
[o, r, d, l, w]
3. binarySearch
查找指定集合中的元素,返回所查找元素的索引
1 public static <T> int binarySearch(List<? extends Comparable<? super T>> list, 2 T key) 3 4 public static <T> int binarySearch(List<? extends T> list, 5 T key, 6 Comparator<? super T> c)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List c = new ArrayList(); 4 c.add("w"); 5 c.add("o"); 6 c.add("r"); 7 c.add("l"); 8 c.add("d"); 9 System.out.println(c); 10 int m = Collections.binarySearch(c, "o"); 11 System.out.println(m); 12 } 13 }
运行结果为:[w, o, r, l, d]
注意:若查找的元素不存在,示例中的n即表示该元素最有可能存在的位置的索引。
4. max
1 public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) 2 3 public static <T> T max(Collection<? extends T> coll, 4 Comparator<? super T> comp)
前者采用Collection内含自然比较法,后者采用Comparator进行比较.
5. min
1 public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) 2 3 public static <T> T min(Collection<? extends T> coll, 4 Comparator<? super T> comp)
前者采用Collection内含自然比较法,后者采用Comparator进行比较。
6. indexOfSubList
查找subList在list中首次出现位置的索引
1 public static int indexOfSubList(List<?> source, 2 List<?> target)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List list = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(list); 5 List subList = Arrays.asList("three four five six".split(" ")); 6 System.out.println(Collections.indexOfSubList(list, subList)); 7 } 8 }
运行结果为:[one, two, three, four, five, six, siven]
7. lastIndexOfSubList
使用与上例方法的使用相同,在此就不做介绍了。
8. replaceAll
替换批定元素为某元素,若要替换的值存在刚返回true,反之返回false
1 public static <T> boolean replaceAll(List<T> list, 2 T oldVal, 3 T newVal)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List list = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(list); 5 List subList = Arrays.asList("three four five six".split(" ")); 6 System.out.println(Collections.replaceAll(list, "siven", "siven eight")); 7 System.out.println(list); 8 } 9 }
运行结果为:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
9. reverse()
反转集合中元素的顺序
public static void reverse(List<?> list)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List list = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(list); 5 Collections.reverse(list); 6 System.out.println(list); 7 } 8 }
运行结果为:
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
10. rotate
集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来
1 public static void rotate(List<?> list, 2 int distance)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List list = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(list); 5 Collections.rotate(list, 1); 6 System.out.println(list); 7 } 8 }
运行结果为:
[one, two, three, four, five, six, siven]
[siven, one, two, three, four, five, six]
11. copy
将集合n中的元素全部复制到m中,并且覆盖相应索引的元素
1 public static <T> void copy(List<? super T> dest, 2 List<? extends T> src)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List m = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(m); 5 List n = Arrays.asList("我 是 复制过来的哈".split(" ")); 6 System.out.println(n); 7 Collections.copy(m,n); 8 System.out.println(m); 9 } 10 }
运行结果为:
[one, two, three, four, five, six, siven]
[我, 是, 复制过来的哈]
[我, 是, 复制过来的哈, four, five, six, siven]
12. swap
交换集合中指定元素索引的位置
1 public static void swap(List<?> list, 2 int i, 3 int j)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List m = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(m); 5 Collections.swap(m, 2, 3); 6 System.out.println(m); 7 } 8 }
运行结果为:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
13. fill
用对象o替换集合list中的所有元素
1 public static <T> void fill(List<? super T> list, 2 T obj)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 List m = Arrays.asList("one two three four five six siven".split(" ")); 4 System.out.println(m); 5 Collections.fill(m, "haha52T25xixi"); 6 System.out.println(m); 7 } 8 }
运行结果为:
[one, two, three, four, five, six, siven]
[haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi, haha52T25xixi]
14. nCopies
返回大小为n的List,List不可改变,其中的所有引用都指向o
1 public static <T> List<T> nCopies(int n, 2 T o)
示例:
1 public class Practice { 2 public static void main(String[] args){ 3 System.out.println(Collections.nCopies(5, "haha")); 4 } 5 }
运行结果为:
[haha, haha, haha, haha, haha]
参考:http://www.360doc.com/content/14/0829/10/15242507_405537400.shtml