java Hashmap Map TreeMap 的几种遍历方式,全网最全,全网最强
package Collec2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test1 {
public static void main(String[] args) {
HashMap<Student, String> hashmap_88 = new HashMap<Student, String>();
hashmap_88.put(new Student("张三", 23), "北京");
hashmap_88.put(new Student("李四", 24), "南京");
HashMap<Student, String> hashmap_99 = new HashMap<Student, String>();
hashmap_99.put(new Student("王五", 23), "上海");
hashmap_99.put(new Student("赵六", 24), "深圳");
HashMap<HashMap<Student, String>, String> hm = new HashMap<HashMap<Student, String>, String>();
hm.put(hashmap_88, "第88期基础班");
hm.put(hashmap_99, "第99期基础班");
System.out.println("遍历方式一");
Set<HashMap<Student, String>> ks = hm.keySet();
Iterator<HashMap<Student, String>> it1 = ks.iterator();
while(it1.hasNext()) {
HashMap<Student,String> next_stu_value = it1.next();
Set<Student> stu_set = next_stu_value.keySet();
Iterator<Student> stu_it = stu_set.iterator();
System.out.println(hm.get(next_stu_value)+"有以下学生");
while(stu_it.hasNext()) {
Student student = stu_it.next();
System.out.println("个人信息"+student);
}
}
System.out.println("遍历方式二");
for (HashMap<Student, String> hashMap : hm.keySet()) {
System.out.println(hm.get(hashMap)+"有以下学生");
for (Student stu : hashMap.keySet()) {
System.out.println("个人信息"+stu);
}
}
System.out.println("遍历方式三");
Set<HashMap<Student,String>> x=hm.keySet();
for(Iterator<HashMap<Student,String>> it= x.iterator();it.hasNext();) {
HashMap<Student, String> stu_map_value = it.next();
System.out.println(hm.get(stu_map_value)+"有以下学生");
for( Iterator<Student> it2 = stu_map_value.keySet().iterator();it2.hasNext();) {
Student student = it2.next();
System.out.println("个人信息"+student);
}
}
System.out.println("遍历方式四");
Set<Entry<HashMap<Student,String>,String>> entrySet = hm.entrySet();
Iterator<Entry<HashMap<Student, String>, String>> itentry = entrySet.iterator();
while(itentry.hasNext()) {
Entry<HashMap<Student, String>, String> entry = itentry.next();
HashMap<Student,String> map = entry.getKey();
System.out.println(entry.getValue()+"有以下学生");
Iterator<Entry<Student, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Entry<Student, String> next = iterator.next();
System.out.println("个人信息"+next.getKey()+" "+next.getValue());
}
}
System.out.println("遍历方式五");
for (Map.Entry<HashMap<Student, String>, String> entry : hm.entrySet()) {
System.out.println(entry.getValue()+"有以下学生");
for (Map.Entry<Student,String> stu : entry.getKey().entrySet()) {
System.out.println("个人信息"+stu.getKey()+" "+stu.getValue());
}
}
System.out.println("遍历方式六");
hm.forEach((K,V)->{
System.out.println(V+"有以下学生");
K.forEach((sub_K,sub_V)->{
System.out.println("个人信息"+sub_K+" "+sub_V);
});
});
Iterator<HashMap<Student, String>> ite2 = hm.keySet().iterator();
while(ite2.hasNext()) {
HashMap<Student,String> next = ite2.next();
Iterator<Entry<Student, String>> iterator = next.entrySet().iterator();
while(iterator.hasNext()) {
Entry<Student, String> entry = iterator.next();
if(entry.getValue().equals("深圳")) {
iterator.remove();
}
}
}
System.out.println("-=-----------------------");
hm.forEach((K,V)->{
System.out.println(V+"有以下学生");
K.forEach((sub_K,sub_V)->{
System.out.println("个人信息"+sub_K+" "+sub_V);
});
});
}
}
知识兔