Justin's Words

Java Collections

惯例,先上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package co.young.DataStructure;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsExample {
public static void main(String[] args) {
List<Double> temperatureList = new ArrayList<Double>();

temperatureList.add(40.5);
temperatureList.add(33.9);
temperatureList.add(37.8);
temperatureList.add(15.3);
temperatureList.add(25.6);

// Print elements of temperatureList
System.out.println(temperatureList);

// Sorting List in ascending order according to the natural ordering
Collections.sort(temperatureList);
System.out.println("Sorted List: "+temperatureList);

// Searching a temperature from list
int searchIndex = Collections.binarySearch(temperatureList, 37.8);
if(searchIndex >=0){
System.out.println("Temperature found at " + searchIndex + ".");
} else{
System.out.println("Temperature not found.");
}

//Shuffles the list
Collections.shuffle(temperatureList);
System.out.println("Shuffled List: "+temperatureList);

//Get maximum temperature from temperatureList
Double max = Collections.max(temperatureList);
System.out.println("Maximun temperature: "+max);

//Get minimum temperature from temperatureList
Double min = Collections.min(temperatureList);
System.out.println("Minimum temperature: "+min);

//Reverse the list
Collections.reverse(temperatureList);
System.out.println("Reversed List: "+temperatureList);

//Copy elements from one list to another
List<Double> newTemperatureList = new ArrayList<Double>(temperatureList.size());
newTemperatureList.add(13.6);
newTemperatureList.add(10.2);
newTemperatureList.add(42.9);
newTemperatureList.add(34.4);
newTemperatureList.add(27.2);
System.out.println("New temperature list: "+newTemperatureList);
Collections.copy(newTemperatureList, temperatureList);
System.out.println("New temperature list after copy: "+newTemperatureList);

//Replaces all occurrences of one specified value in a list with another.
Collections.replaceAll(temperatureList, 40.5, 0.0);
System.out.println("After replaceAll: "+temperatureList);

//Fill temperatureList.
Collections.fill(temperatureList, 0.0);
System.out.println("Filled List: "+temperatureList);
}
}

方法分析

跳去关于 ArrayList 的博文。

按升序排序列表

格式:

1
static <T> void sort(List<T> list, Comparator<? super T> c);

使用:

1
Collections.sort(temperatureList);

查找指定元素所在的位置

格式:

1
static <T> int binarySearch(List<? extend Comparable<? super T>> list, T key);

使用:

1
2
3
4
5
6
int searchIndex = Collections.binarySearch(temperatureList, 37.8);
if(searchIndex >=0){
System.out.println("Temperature found at " + searchIndex + ".");
} else{
System.out.println("Temperature not found.");
}

打乱列表元素顺序

shuffle,洗牌。

格式:

1
static void shuffle(List<?> list);

使用:

1
2
3
4
5
6
int searchIndex = Collections.binarySearch(temperatureList, 37.8);
if(searchIndex >=0){
System.out.println("Temperature found at " + searchIndex + ".");
} else{
System.out.println("Temperature not found.");
}

获取列表最大值和获取列表最小值

格式:

1
2
static <T extends Object & Comparable<? super T>> max(Collection<? extends T> coll);
static <T extends Object & Comparable<? super T>> min(Collection<? extends T> coll)

使用:

1
2
3
4
5
6
7
//Get maximum temperature from temperatureList
Double max = Collections.max(temperatureList);
System.out.println("Maximun temperature: "+max);

//Get minimum temperature from temperatureList
Double min = Collections.min(temperatureList);
System.out.println("Minimum temperature: "+min);

倒转列表元素排序

格式:

1
static void reverse(List<?> list);

使用:

1
2
3
//Reverse the list
Collections.reverse(temperatureList);
System.out.println("Reversed List: "+temperatureList);

复制列表

格式:

1
public static <T> void copy(List<? super T> dest, List<? extends T> src);

使用:

1
2
3
4
5
6
7
8
9
10
//Copy elements from one list to another
List<Double> newTemperatureList = new ArrayList<Double>(temperatureList.size());
newTemperatureList.add(13.6);
newTemperatureList.add(10.2);
newTemperatureList.add(42.9);
newTemperatureList.add(34.4);
newTemperatureList.add(27.2);
System.out.println("New temperature list: "+newTemperatureList);
Collections.copy(newTemperatureList, temperatureList);
System.out.println("New temperature list after copy: "+newTemperatureList);

输出:

1
2
New temperature list: [13.6, 10.2, 42.9, 34.4, 27.2]
New temperature list after copy: [25.6, 33.9, 40.5, 15.3, 37.8]

替换指定元素

格式:

1
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal);

使用:

1
2
3
//Replaces all occurrences of one specified value in a list with another.
Collections.replaceAll(temperatureList, 40.5, 0.0);
System.out.println("After replaceAll: "+temperatureList);

输出:

1
After replaceAll: [25.6, 33.9, 0.0, 15.3, 37.8]

给列表充满指定元素

格式:

1
public static <T> void fill(List<? super T> list, T obj)

使用:

1
2
3
//Fill temperatureList.
Collections.fill(temperatureList, 0.0);
System.out.println("Filled List: "+temperatureList);

输出:

1
Filled List: [0.0, 0.0, 0.0, 0.0, 0.0]