java中map可以按插入顺序倒序输出么?
问题描述
public class Test { public static void main(String[] args) {LinkedHashMap<String,String> a=new LinkedHashMap<String,String>();a.put('a', '1');a.put('b', '2');a.put('c', '3');for(String b:a.keySet()){ System.out.println(b+'值为:'+a.get(b));} }}
输出为a值为:1b值为:2c值为:3想要倒序输出怎么实现
问题解答
回答1:public class Test {
public static void main(String[] args) { LinkedHashMap<String,String> linkedhashmap = new LinkedHashMap<String,String>(); linkedhashmap .put('a', '1'); linkedhashmap .put('b', '2'); linkedhashmap .put('c', '3'); ListIterator<Map.Entry<String,String>> i = new ArrayList<Map.Entry<String,String>>(linkedhashmap.entrySet()).listIterator(linkedhashmap.size());while(i.hasPrevious()) { Map.Entry<String, String> entry=i.previous(); System.out.println(entry.getKey()+':'+entry.getValue()); } }
}
相关文章:
1. windows误人子弟啊2. 冒昧问一下,我这php代码哪里出错了???3. MySQL主键冲突时的更新操作和替换操作在功能上有什么差别(如图)4. python - linux怎么在每天的凌晨2点执行一次这个log.py文件5. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。6. 实现bing搜索工具urlAPI提交7. mysql优化 - MySQL如何为配置表建立索引?8. 如何用笔记本上的apache做微信开发的服务器9. 我在网址中输入localhost/abc.php显示的是not found是为什么呢?10. 关于mysql联合查询一对多的显示结果问题
