我有一个我正在使用的EnumMap,我将需要保留我传入的项目的顺序.我知道在使用HashMap时,我可以初始化LinkedHashMap以保持顺序,如下所示:
HashMap> contentTypeToIdList = new LinkedHashMap>();
但是,我想使用EnumMap.我怎么能做这样的事情:
EnumMap> contentTypeToIdList = new LinkedHashMap>();
解决方法:
正如API告诉我们:
Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collections views (keySet(), entrySet(), and values()).
这意味着它是内部排序的,根本不应该以不同方式排序,例如您需要的插入顺序.
标签:java
来源: https://codeday.me/bug/20190522/1154082.html