关于java:排序国家/地区列表

Sort list of countries

本问题已经有最佳答案,请猛点这里访问。

我想生成排序列表中所有国家/地区的列表。 我试过这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public Map<String, String> getCountryNameCodeList() {

        String[] countryCodes = Locale.getISOCountries();

        Arrays.sort(countryCodes);

        Map<String, String> list = new HashMap<>();

        for (String countryCode : countryCodes) {

            Locale obj = new Locale("", countryCode);

            list.put(obj.getDisplayCountry(), obj.getCountry());
        }

        return list;
    }

但由于某种原因,该列表未分类。 坚持下去的正确方法是什么?


HashMap未排序,使用LinkedHashMap

This implementation differs from HashMap in that it maintains a
doubly-linked list running through all of its entries. This linked
list defines the iteration ordering, which is normally the order in
which keys were inserted into the map (insertion-order).

只是改变

1
Map<String, String> list = new HashMap<>();

至:

1
Map<String, String> list = new LinkedHashMap<>();