闲来无事,学习java基础–Map
package com.crazzy.day01
import java.util.HashMap
import java.util.Iterator
import java.util.Map
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>()
map.put("2","1995")
map.put("3","2005")
map.put("1","1985")
System.out.println("第一种遍历(普遍):")
for(String key : map.keySet()){
System.out.println("key -> " + key + ": value -> " + map.get(key))
}
System.out.println("第二种遍历(迭代器):")
Iterator<Map.Entry<String,String>> it = map.entrySet().iterator()
while (it.hasNext()){
Map.Entry<String,String> entry = it.next()
System.out.println("key ->" + entry.getKey() + ": value ->" + entry.getValue())
}
System.out.println("第三种方法(大容量):")
for (Map.Entry<String,String> entry : map.entrySet()){
System.out.println("key ->" + entry.getKey() + ": value ->" + entry.getValue())
}
}
}
运行结果:
![这里写图片描述](https://img-blog.csdn.net/20170920161449781?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlucWlnYW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
可以看出,map会按照key的值自动进行排序。