Collections class
数组 —> Arrays
Collection —> Collections
排序
public class Main {
public static void main(java.lang.String[] args) {
String mercury = new String("Mercury");
String venus = new String("Venus");
String earth = new String("Earth");
String mars = new String("Mars");
String jupiter = new String("Jupiter");
String saturn = new String("Saturn");
String uranus = new String("Uranus");
String neptune = new String("Neptune");
ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
jupiter, saturn, uranus, neptune));
Collections.sort(solarSystem);
System.out.println(solarSystem);
}
}
Output:_**[Earth, Jupiter, Mars, Mercury, Neptune, Saturn, Uranus, Venus]**_
_
最大最小
public static void main(java.lang.String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
System.out.println(Collections.max(numbers));
System.out.println(Collections.min(numbers));
}
反转
public class Main {
public static void main(java.lang.String[] args) {
String mercury = new String("Mercury");
String venus = new String("Venus");
String earth = new String("Earth");
String mars = new String("Mars");
String jupiter = new String("Jupiter");
String saturn = new String("Saturn");
String uranus = new String("Uranus");
String neptune = new String("Neptune");
ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
jupiter, saturn, uranus, neptune));
Collections.sort(solarSystem);
Collections.reverse(solarSystem);
System.out.println(solarSystem);
}
}
Output:
[Venus, Uranus, Saturn, Neptune, Mercury, Mars, Jupiter, Earth]
_
乱序
public class Main {
public static void main(java.lang.String[] args) {
ArrayList<Integer> bingoDrum = new ArrayList<>(100);
for (int i = 1; i <= 100; i++) {
bingoDrum.add(i);// add the numbers 1 to 100 to the drum
}
Collections.shuffle(bingoDrum);// Mix it up
System.out.println ("Your attention, please! Here are the first 10 numbers from the drum!");
for (int i = 0; i < 10; i++) {
System.out.println(bingoDrum.get(i));
}
}
}
Output:
Your attention, please! Here are the first 10 numbers from the drum!
32
61
4
81
25
8
66
35
42
71
_
交换
public class Main {
public static void main(java.lang.String[] args) {
String mercury = new String("Mercury");
String venus = new String("Venus");
String earth = new String("Earth");
String mars = new String("Mars");
String jupiter = new String("Jupiter");
String saturn = new String("Saturn");
String uranus = new String("Uranus");
String neptune = new String("Neptune");
ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(neptune, venus, earth, mars
, jupiter, saturn, uranus, mercury));// The planets are in the wrong order
System.out.println(solarSystem);
Collections.swap(solarSystem, solarSystem.indexOf(mercury), solarSystem.indexOf(neptune));
System.out.println(solarSystem);
}
}
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
为第一个大于等于被查找元素的索引。