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 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.List; 5 6 public class SortTest { 7 public static void main(String[] args) { 8 List<String> lists = new ArrayList<String>(); 9 List<A> list = new ArrayList<A>(); 10 List<B> listB = new ArrayList<B>(); 11 lists.add("5"); 12 lists.add("2"); 13 ists.add("9"); //lists中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序 14 Collections.sort(lists); 15 16 A aa = new A(); 17 a.setName("aa"); 18 aa.setOrder(1); 19 A bb = new A(); 20 bb.setName("bb"); 21 bb.setOrder(2); 22 list.add(bb); 23 list.add(aa); //list中的对象A实现Comparable接口 24 Collections.sort(list); 25 26 B ab = new B(); 27 ab.setName("ab"); 28 ab.setOrder("1"); 29 B ba = new B(); 30 ba.setName("ba"); 31 ba.setOrder("2"); 32 listB.add(ba); 33 listB.add(ab); //根据Collections.sort重载方法来实现 34 Collections.sort(listB,new Comparator<B>(){ 35 @Override 36 public int compare(B b1, B b2) { 37 return b1.getOrder().compareTo(b2.getOrder()); 38 } 39 40 }); 41 42 System.out.println(lists); 43 System.out.println(list); 44 System.out.println(listB); 45 } 46 } 47 48 class A implements Comparable<A>{ 49 private String name; 50 private Integer order; 51 public String getName() { 52 return name; 53 } 54 public void setName(String name) { 55 this.name = name; 56 } 57 public Integer getOrder() { 58 return order; 59 } 60 public void setOrder(Integer order) { 61 this.order = order; 62 } 63 @Override 64 public String toString() { 65 return "name is "+name+" order is "+order; 66 } 67 @Override 68 public int compareTo(A a) { 69 return this.order.compareTo(a.getOrder()); 70 } 71 } 72 73 class B{ 74 private String name; 75 private String order; 76 public String getName() { 77 return name; 78 } 79 public void setName(String name) { 80 this.name = name; 81 } 82 public String getOrder() { 83 return order; 84 } 85 public void setOrder(String order) { 86 this.order = order; 87 } 88 @Override 89 public String toString() { 90 return "name is "+name+" order is "+order; 91 } 92 }
打印的结果为:
[2, 5, 9] [name is aa order is 1, name is bb order is 2] [name is ab order is 1, name is ba order is 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