java hashmap遍历顺序_史上最完整的集合类总结及hashMap遍历
一.java集合类的比较:
二、HashMap的遍历共有两种:
1.利用entrySet 键值对映射:
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while(it.hashNext()){
Map.Entry s = (Map.Entry)it.next();
System.out.println(s.getKey());
System.out.println(s.getValue());
}
2.利用keySet:
Map map = new HashMap();
Iterator it = map.keySet().iterator();
while(it.hasNext()){
Object key = it.next();
System.out.println(key);
System.out.println(map.get(key));
}
LinkedHashMap的遍历,保证读取数据的顺序和put的顺序一致:
/**
-
LinkedHashMap倒序
-
@author zzw
*/
public class LinkedHashMapSort {
public static void main(String[] args) {
LinkedHashMap linkedhashmap = new LinkedHashMap();
linkedhashmap.put("1","a");
linkedhashmap.put("2","b");
linkedhashmap.put("3","c");
linkedhashmap.put("4","d");
ListIterator> i=new ArrayList>
(linkedhashmap.entrySet()).listIterator(linkedhashmap.size());
while(i.hasPrevious()) {
Map.Entry entry=i.previous();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
————————————————
版权声明:本文为CSDN博主「荼蘼夜未央」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_34221599/article/details/114191381