import java.util.*;
public class MapText {
public static void main(String[] args) {
Map map = new HashMap(); //由HashMap实现的Map对象
Student stu = new Student("001", "小葛");
Student stu2 = new Student("005", "小陈"); //创建Student对象
Student stu3 = new Student("004", "小赵");
map.put(stu.getS_id(), stu.getS_name());
map.put(stu2.getS_id(), stu2.getS_name()); //将对象添加到集合中
map.put(stu3.getS_id(), stu3.getS_name());
Set set = map.keySet(); //获取Map集合中的key对象集合
Iterator it = set.iterator();
System.out.println("HashMap类实现的Map集合,内容如下:");
while (it.hasNext()) {
String str = (String) it.next();
String name =(String) map.get(str); //获取指定key的value值
System.out.println(str + " "+name);
}
TreeMap treemap = new TreeMap(); //创建TreeMap集合对象
treemap.putAll(map); //向集合添加对象
Iterator iter = treemap.keySet().iterator();
System.out.println("TreeMap类实现的Map集合,键对象升序如下:");
while (iter.hasNext()) { //遍历TreeMap集合对象
String str = (String) iter.next(); //获取集合中的所有key对象
String name =(String) map.get(str); //获取指定key的value值
System.out.println(str+" "+name);
}
}
}
------解决方案--------------------------------------------------------
谷歌或百度:泛型
------解决方案--------------------------------------------------------
Map<,> map = new HashMap<,>();