Finology 大数据金融

通过大数据以量化金融

Springboot 2.1

在测试类上面写的注解如下:

1
2
3
4
5
6
7
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class AppTests {

@Test
public void test() {}
}

需要注意的是,Application 是 Spring boot 的启动类,test() 方法必须是 public 类型。

Springboot 2.2

注解如下:

1
2
3
4
5
6
@SpringBootTest
public class AppTests {

@Test
void test() {}
}

不需要写 @RunWith 注解和指定 SpringBootTest 的 classes 属性。test() 方法不用写 public 类型。

把 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 中,就可以排好序了。例子下回再讲。

0%