package com.yejunli.util.ForDemo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @auther: YeZhanCHN
* @Date: 2021/7/18 - 07 - 18 - 16:15}
* @Description: Map集合的练习
* @version:1.0
*/
public class DemoMap {
public static void main(String[] args) {
//1. 创建map集合的对象
Map<Integer, Students> map = new HashMap<>();
//2. 创建元素对象
Students s1 = new Students("张1", 142, 104741);
Students s2 = new Students("张2", 132, 104742);
Students s3 = new Students("张2", 122, 104743);
Students s4 = new Students("张4", 122, 104744);
Students s5 = new Students("张4", 112, 104745);
Students s6 = new Students("张6", 12452, 104746);
Students s7 = new Students("张7", 152, 104747);
//3. 将元素添加到Map集合
/*Students stu1 = map.put(1, s1);
Students stu2 = map.put(2, s2);
Students stu3 = map.put(3, s3);
Students stu4 = map.put(4, s4);
Students stu5 = map.put(5, s5);
Students stu6 = map.put(6, s6);
Students stu7 = map.put(7, s7);*/
map.put(1,s1);
map.put(2,s2);
map.put(3,s3);
map.put(4,s4);
map.put(5,s5);
map.put(6,s6);
map.put(7,s7);
//根据键获取值
System.out.println("根据键Key1获取值:"+map.get(1));
//System.out.println("sru1:"+s1);
System.out.println(map);
System.out.println("用增强for循环遍历map集合");
//4. 遍历map集合
//4.1 用增强for循环遍历map集合
for (Students value : map.values()) {
System.out.println(value);
}
System.out.println("____________用迭代器遍历map集合____________");
//4.2 用迭代器遍历map集合
//4.2.1 先获取所有键的集合
Set<Integer> mapSet = map.keySet();
//4.2.2 迭代
Iterator<Integer> iterator = mapSet.iterator();
while (iterator.hasNext()){
Integer key = iterator.next();
Students students = map.get(key);
System.out.println("key:"+key+".......value:"+students);
}
}
}
package com.yejunli.util.ForDemo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @auther: YeZhanCHN
* @Date: 2021/7/18 - 07 - 18 - 16:15}
* @Description: Map集合的练习
* @version:1.0
*/
public class DemoMap {
public static void main(String[] args) {
//1. 创建map集合的对象
Map<Integer, Students> map = new HashMap<>();
//2. 创建元素对象
Students s1 = new Students("张1", 142, 104741);
Students s2 = new Students("张2", 132, 104742);
Students s3 = new Students("张2", 122, 104743);
Students s4 = new Students("张4", 122, 104744);
Students s5 = new Students("张4", 112, 104745);
Students s6 = new Students("张6", 12452, 104746);
Students s7 = new Students("张7", 152, 104747);
//3. 将元素添加到Map集合
/*Students stu1 = map.put(1, s1);
Students stu2 = map.put(2, s2);
Students stu3 = map.put(3, s3);
Students stu4 = map.put(4, s4);
Students stu5 = map.put(5, s5);
Students stu6 = map.put(6, s6);
Students stu7 = map.put(7, s7);*/
map.put(1,s1);
map.put(2,s2);
map.put(3,s3);
map.put(4,s4);
map.put(5,s5);
map.put(6,s6);
map.put(7,s7);
//根据键获取值
System.out.println("根据键Key1获取值:"+map.get(1));
//System.out.println("sru1:"+s1);
System.out.println(map);
System.out.println("用增强for循环遍历map集合");
//4. 遍历map集合
//4.1 用增强for循环遍历map集合
/*for (Students value : map.values()) {
System.out.println(value);
}*/
Set<Integer> keys = map.keySet();
for (Integer key : keys) {
Students students = map.get(key);
System.out.println("key:"+key+"_________"+"value:"+students);
}
System.out.println("____________用迭代器遍历map集合____________");
//4.2 用迭代器遍历map集合
//4.2.1 先获取所有键的集合
Set<Integer> mapSet = map.keySet();
//4.2.2 迭代
Iterator<Integer> iterator = mapSet.iterator();
while (iterator.hasNext()){
Integer key = iterator.next();
Students students = map.get(key);
System.out.println("key:"+key+".......value:"+students);
}
}
}