Finology 大数据金融

通过大数据以量化金融

把 Map 中的元素按 key 进行排序,然后把 value 取出来组成一个 List。可以按如下方法操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MapSort2ListDemo {

public static void main(String[] args) {

Map<String, String> map = new HashMap<>();
map.put("1", "A");
map.put("3", "C");
map.put("2", "B");
map.put("10", "G");
map.put("11", "H");

List<String> list = map.entrySet().stream()
.sorted(Comparator.comparing((Map.Entry<String, String> entry) -> Integer.parseInt(entry.getKey())).reversed())
.map(x -> x.getValue())
.collect(Collectors.toList());
System.out.println(list);
}
}

结果:[H, G, C, B, A]

我们经常使用 HashMap, 但有时候需要根据 key 进行排序。

通过 Collections.sort()

Map 的 key 是字符串类型的,需要先把字符串转成数字型。使用 reversed() 方法可以让序列倒序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MapSortDemo {

public static void main(String[] args) {

Map<String, String> map = new HashMap<>();
map.put("1", "A");
map.put("3", "C");
map.put("2", "B");
map.put("10", "G");
map.put("11", "H");

List<Map.Entry<String, String>> entries = new ArrayList<>(map.entrySet());
System.out.println(entries);

Collections.sort(entries, Comparator.comparing((Map.Entry<String, String> entry) -> Integer.parseInt(entry.getKey())));
System.out.println(entries);

Collections.sort(entries, Comparator.comparing((Map.Entry<String, String> entry) -> Integer.parseInt(entry.getKey())).reversed());
System.out.println(entries);
}
}

通过 TreeSet 方式

遍历 HashMap 中所有元素,放入 TreeSet 中,就可以排好序了。例子下回再讲。

Spring Boot Actuator 是 Spring Boot 的一套监控系统。

依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

顺便提一句,spring boot 的官方依赖,一般是 spring-boot-starter-xxxx 这种格式的。非官方依赖则是 xxxx-spring-boot-starter 这种格式的。

启动项目。

访问 http://localhost:8080/actuator

可以看到有两个endpoints,一个是 health, 一个是 info。

访问 health:

http://localhost:8080/actuator/health

1
2
3
{
status: "UP"
}

访问 info:

http://localhost:8080/actuator/info

1
{ }

是一个空对象。

我们可以在 application.yml 配置文件中做如下配置:

1
2
3
4
5
6
7
8
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: metrics, health, info

management.endpoint.health.show-details=always 表示在访问 health 时,除了显示 UP 这个状态以外,还要显示更多的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
status: "UP",
components: {
db: {
status: "UP",
details: {
database: "MySQL",
result: 1,
validationQuery: "/* ping */ SELECT 1"
}
},
diskSpace: {
status: "UP",
details: {
total: 250790436864,
free: 18968088576,
threshold: 10485760
}
},
ping: {
status: "UP"
}
}
}

endpoints.web.exposure.include=metrics, health, info 这个表示可以显示的endpoint。

这个值可以填写 *,但是,如果是yaml文件,必须是'*'',而 application.properties 可以直接写 *。这是 yaml 文件和 properties 文件的区别。

我们可以设置 info:

1
2
3
info:
app-name: springboot-mybatis-plus
author: simon

这样,在访问 info endpoint 时,就可以显示上面配置的信息了。

1
2
3
4
{
app-name: "springboot-mybatis-plus",
author: "simon"
}

如果 yaml 里面带了环境变量,在开发时,可以在 IDEA 里面设置。在控制台运行时,命令如下:

java -jar xxx.jar --SOME_ENV=certain_env

0%