hutool List<Map<Interget,String>> {1,深圳},{2,东莞},{3,莞},{4,惠州} 地点排序,河源、深圳、惠州、中山、东莞
时间: 2024-02-09 21:13:35 浏览: 34
您可以使用Java的Collections.sort()方法来对List<Map<Integer, String>>进行排序,具体实现如下:
```
List<Map<Integer, String>> list = new ArrayList<>();
Map<Integer, String> map1 = new HashMap<>();
map1.put(1, "深圳");
Map<Integer, String> map2 = new HashMap<>();
map2.put(2, "东莞");
Map<Integer, String> map3 = new HashMap<>();
map3.put(3, "莞");
Map<Integer, String> map4 = new HashMap<>();
map4.put(4, "惠州");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
Collections.sort(list, new Comparator<Map<Integer, String>>() {
@Override
public int compare(Map<Integer, String> o1, Map<Integer, String> o2) {
String[] arr = {"河源", "深圳", "惠州", "中山", "东莞"};
int index1 = -1;
int index2 = -1;
for (int i = 0; i < arr.length; i++) {
if (o1.containsValue(arr[i])) {
index1 = i;
}
if (o2.containsValue(arr[i])) {
index2 = i;
}
}
return index1 - index2;
}
});
```
上述代码中,我们定义了一个String类型的数组,其中存储了地点的排序规则,然后使用Comparator接口的compare()方法来对List进行排序。在compare()方法中,我们遍历了地点的排序规则数组,然后通过containsValue()方法来判断Map中是否包含该地点,如果包含则记录下该地点在数组中的索引,最终返回两个Map在数组中的索引差,从而完成排序。
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)