课堂笔记:
Map集合中存储map集合或任意集合的嵌套形式。
练习动作 存 取 熟悉泛型。
package homework;
import java.util.*;
import java.util.Map.Entry;
/*练习嵌套集合存储 取出
* 已传智播客为题 分别有java预热班和UI预热班 每个班里面都有各自的学生
*
* */
class Save {// 记录存入的值,并已字符串形式返回。
private String id;
private String name;
Save(String id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return id + "<---->" + name;
}
}
public class MapDemo {
public static void main(String[] args) {
TreeMap<String, List<Save>> czbk = new TreeMap<String, List<Save>>();// 嵌套容器
List<Save> Javayureban = new ArrayList<Save>();// list容器
List<Save> Uiyureban = new ArrayList<Save>();
czbk.put("JAVA", Javayureban);
czbk.put("UI", Uiyureban);
Javayureban.add(new Save("01", "LaoWang"));
Javayureban.add(new Save("02", "XiaoWang"));
Uiyureban.add(new Save("01", "LaoLi"));
Uiyureban.add(new Save("02", "XiaoLi"));
/*
* Iterator<Map.Entry<String,
* List<Save>>>it=czbk.entrySet().iterator();//entrySet取出方法
* while(it.hasNext()){ Map.Entry<String, List<Save>>
* hmc=it.next();//取出键值对 String kc=hmc.getKey(); List<Save> bj
* =hmc.getValue(); //分别获取键和值 System.out.println(kc+bj); }
*/
Iterator<String> it = czbk.keySet().iterator();// KeySet取出方法
while (it.hasNext()) {
String kc = it.next();
List<Save> bj = czbk.get(kc);// 用czbk容器的get(KEY)方法取出与Key存在映射关系的Value.
/*本来这里想提问来的后来看到了编译软件注解知道了原来取出的正是和key想匹配的value值
get
public V get(Object key)Returns the value to which the specified key is mapped,
or null if this map contains no mapping for the key.
中文意思:public V get(Object key)返回指定键映射到的值,如果此映射不包含密钥映射,则为null。
*/
System.out.println(kc);
getInfo(bj);
}
}
public static void getInfo(List<Save> list) {// 遍历list容器方法
Iterator<Save> li = list.iterator();
while (li.hasNext()) {
Save s = li.next();
System.out.println(s);
}
// TreeMap存储方法
TreeMap<String, TreeMap<String, String>> czbk = new TreeMap<String, TreeMap<String, String>>();
TreeMap<String, String> javayure = new TreeMap<String, String>();
TreeMap<String, String> uiyure = new TreeMap<String, String>();
czbk.put("Java_YuRe_Ban", javayure);
czbk.put("UI_YuRe_Ban", uiyure);
javayure.put("01", "wangxin");
javayure.put("02", "rap");
uiyure.put("01", "xiha");
uiyure.put("02", "abc"); //
Set<Map.Entry<String, TreeMap<String, String>>> it2 = (Set<Map.Entry<String, TreeMap<String, String>>>) czbk
.entrySet().iterator();
// 这迭代器调用的太牛逼了
Iterator<Entry<String, TreeMap<String, String>>> it = czbk.entrySet().iterator();//entrySet取出
while (it.hasNext()) {//迭代器取出
Map.Entry<String, TreeMap<String, String>> xs = it.next();
String bj = xs.getKey();
TreeMap xsh = xs.getValue();
System.out.println("[" + bj + "]" + "=====" + xsh);
}
getStudnetInfo(javayure);
getStudnetInfo(uiyure);
}
public static void getStudnetInfo(TreeMap<String, String> tm) {//创建一个取出方法传啥取啥
Iterator<Map.Entry<String, String>> it2 = tm.entrySet().iterator();//关键调用自己的迭代器
while (it2.hasNext()) {
Map.Entry<String, String> pair = it2.next();
String number = pair.getKey();
String name = pair.getValue();
System.out.println("[" + number + "]" + "----->" + name);
}
}
}
|
|