現在有一個map集合如下: Map<Integer,String> map = new HashMap<Integer, String>(); map.put(1, “
阿新 • • 發佈:2021-02-12
技術標籤:java
程式碼
import java.util.*; /*4. 現在有一個map集合如下: Map<Integer,String> map = new HashMap<Integer, String>(); map.put(1, "張三丰"); map.put(2, "周芷若"); map.put(3, "汪峰"); map.put(4, "滅絕師太"); 要求: 1.遍歷集合,並將序號與對應人名列印。 2.向該map集合中插入一個編碼為5姓名為郭靖的資訊 3.移除該map中的編號為1的資訊 4.將map集合中編號為2的姓名資訊修改為"周林"*/ public class Homework4 { public static void main(String[] args) { Map<Integer,String> map = new HashMap<Integer, String>(); map.put(1, "張三丰"); map.put(2, "周芷若"); map.put(3, "汪峰"); map.put(4, "滅絕師太"); //1.遍歷集合,並將序號與對應人名列印。 Set<Integer> set = map.keySet(); for (int i : set){ System.out.println(i + "=" + map.get(i)); } //2.向該map集合中插入一個編碼為5姓名為郭靖的資訊 map.put(5, "郭靖"); //3.移除該map中的編號為1的資訊 map.remove(1); //4.將map集合中編號為2的姓名資訊修改為"周林" map.put(2, "周林"); Set<Map.Entry<Integer, String>> set1 = map.entrySet(); for (Map.Entry<Integer, String> me : set1){ System.out.println(me.getKey() + "=" + me.getValue()); } } }