package ch2;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* Map的复习使用,遍历
*
* @author szw
*
*/
public class MapDemo {
public static void main(String[] args) {
/*
* 遍历Hash表的时候是没有顺序的
* 优点:时间复杂度为1,只做一次hash运算
*/
// Map<String,Student> stuMap = new HashMap<String,Student>();
/*
* LinkedHashMap对插入数据的顺序进行记录,按照插入顺序排序
*/
// Map<String,Student> stuMap = new LinkedHashMap<String,Student>();
/*
* TreeMap对数据根据键进行排序
*/
Map<String,Student> stuMap = new TreeMap<String,Student>();
stuMap.put("1234", new Student("1234","王小明"));
stuMap.put("1235", new Student("1235","张三"));
stuMap.put("1236", new Student("1236","李四"));
for(Map.Entry<String, Student> en:stuMap.entrySet()){
System.out.println(en.getKey()+" "+en.getValue());
}
}
}
Map的遍历
最新推荐文章于 2024-08-04 16:42:24 发布