Java HashMap按键插入时间排序-创建顺序
我想对hashmap进行排序或按创造性顺序将所有键添加到arraylist 例如:Java HashMap按键插入时间排序-创建顺序,java,hashmap,Java,Hashmap,我想对hashmap进行排序或按创造性顺序将所有键添加到arraylist 例如: HashMap<String, String> map_from_file = new HashMap<String, String>(); map_from_file.put("C", "c"); map_from_file.put("B", "b");
HashMap<String, String> map_from_file = new HashMap<String, String>();
map_from_file.put("C", "c");
map_from_file.put("B", "b");
map_from_file.put("D", "d");
map_from_file.put("A", "a");
for (String key : map_from_file.keySet()) {
System.out.println(key);
}
我希望输出为:
C
B
D
A
感谢您的帮助Map\u from\u file=new LinkedHashMap();
Map<String, String> map_from_file = new LinkedHashMap<>();
map_from_file.put("C", "c");
map_from_file.put("B", "b");
map_from_file.put("D", "d");
map_from_file.put("A", "a");
for (String key : map_from_file.keySet()) {
System.out.println(key);
}
从_文件映射_.put(“C”、“C”);
从_文件映射_.put(“B”,“B”);
从_文件映射_.put(“D”,“D”);
映射来自_文件。put(“A”,“A”);
for(字符串键:映射来自\u文件.keySet()的\u){
系统输出打印项次(键);
}
您必须使用LinkedHashMap
数据结构作为已排序密钥。这可能是一条注释。
Map<String, String> map_from_file = new LinkedHashMap<>();
map_from_file.put("C", "c");
map_from_file.put("B", "b");
map_from_file.put("D", "d");
map_from_file.put("A", "a");
for (String key : map_from_file.keySet()) {
System.out.println(key);
}