Collections class
数组 —> Arrays
Collection —> Collections

排序

  1. public class Main {
  2. public static void main(java.lang.String[] args) {
  3. String mercury = new String("Mercury");
  4. String venus = new String("Venus");
  5. String earth = new String("Earth");
  6. String mars = new String("Mars");
  7. String jupiter = new String("Jupiter");
  8. String saturn = new String("Saturn");
  9. String uranus = new String("Uranus");
  10. String neptune = new String("Neptune");
  11. ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
  12. jupiter, saturn, uranus, neptune));
  13. Collections.sort(solarSystem);
  14. System.out.println(solarSystem);
  15. }
  16. }

Output:_**[Earth, Jupiter, Mars, Mercury, Neptune, Saturn, Uranus, Venus]**_
_

最大最小

  1. public static void main(java.lang.String[] args) {
  2. ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
  3. System.out.println(Collections.max(numbers));
  4. System.out.println(Collections.min(numbers));
  5. }

Output:
7
1
_

反转

  1. public class Main {
  2. public static void main(java.lang.String[] args) {
  3. String mercury = new String("Mercury");
  4. String venus = new String("Venus");
  5. String earth = new String("Earth");
  6. String mars = new String("Mars");
  7. String jupiter = new String("Jupiter");
  8. String saturn = new String("Saturn");
  9. String uranus = new String("Uranus");
  10. String neptune = new String("Neptune");
  11. ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
  12. jupiter, saturn, uranus, neptune));
  13. Collections.sort(solarSystem);
  14. Collections.reverse(solarSystem);
  15. System.out.println(solarSystem);
  16. }
  17. }

Output:
[Venus, Uranus, Saturn, Neptune, Mercury, Mars, Jupiter, Earth]
_

乱序

  1. public class Main {
  2. public static void main(java.lang.String[] args) {
  3. ArrayList<Integer> bingoDrum = new ArrayList<>(100);
  4. for (int i = 1; i <= 100; i++) {
  5. bingoDrum.add(i);// add the numbers 1 to 100 to the drum
  6. }
  7. Collections.shuffle(bingoDrum);// Mix it up
  8. System.out.println ("Your attention, please! Here are the first 10 numbers from the drum!");
  9. for (int i = 0; i < 10; i++) {
  10. System.out.println(bingoDrum.get(i));
  11. }
  12. }
  13. }

Output:
Your attention, please! Here are the first 10 numbers from the drum!
32
61
4
81
25
8
66
35
42
71
_

交换

  1. public class Main {
  2. public static void main(java.lang.String[] args) {
  3. String mercury = new String("Mercury");
  4. String venus = new String("Venus");
  5. String earth = new String("Earth");
  6. String mars = new String("Mars");
  7. String jupiter = new String("Jupiter");
  8. String saturn = new String("Saturn");
  9. String uranus = new String("Uranus");
  10. String neptune = new String("Neptune");
  11. ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(neptune, venus, earth, mars
  12. , jupiter, saturn, uranus, mercury));// The planets are in the wrong order
  13. System.out.println(solarSystem);
  14. Collections.swap(solarSystem, solarSystem.indexOf(mercury), solarSystem.indexOf(neptune));
  15. System.out.println(solarSystem);
  16. }
  17. }

Output:
[Neptune, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Mercury]
[Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune]
_

binarySearch

static <T> int **[binarySearch](../../java/util/Collections.html#binarySearch-java.util.List-T-)**([List](../../java/util/List.html)<? extends [Comparable](../../java/lang/Comparable.html)<? super T>> list, T key)此方法传入一个实现了Comparable接口的对象类的列表和要查找的元素。
static <T> int **[binarySearch](../../java/util/Collections.html#binarySearch-java.util.List-T-java.util.Comparator-)**([List](../../java/util/List.html)<? extends T> list, T key, [Comparator](../../java/util/Comparator.html)<? super T> c)此方法传入一个实现了列表,要查找的元素和一个比较器。

在进行此调用之前,必须根据列表元素的自然顺序对列表进行升序排序(通过 sort(List) 方法)。

也不一定是升序,对列表进行排序的比较器和二分使用的比较器为同一种即可

如果没有对列表进行排序,则结果是不确定的。
如果列表包含多个等于指定对象的元素,则无法保证找到的是哪一个。

如果列表中存在要查找的元素,返回其索引(0 <= index < list.size())
如果不存在,返回(-(index + 1)),其中index为其应该插入的位置,也就是说如果列表为升序排列,index为第一个大于等于被查找元素的索引。