How do I add the data in the array to the map?
package zh.codegym.task.task08.task0803;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/*
包含猫的 HashMap
*/
public class Solution {
public static void main(String[] args) throws Exception {
String[] cats = new String[]{"泰格", "米西", "斯摩基", "果酱", "奥斯卡", "雪球", "博斯", "斯马奇", "马克斯", "辛巴"};
HashMap<String, Cat> map = addCatsToMap(cats);
for (Map.Entry<String, Cat> pair : map.entrySet()) {
System.out.println(pair.getKey() + " - " + pair.getValue());
}
}
public static HashMap<String, Cat> addCatsToMap(String[] cats) {
HashMap<String, Cat> map = new HashMap<String, Cat>();
map.put("泰格", "");
map.put("米西", "");
map.put("斯摩基", "");
map.put("果酱", "");
map.put("奥斯卡", "");
map.put("雪球", "");
map.put("博斯", "");
map.put("斯马奇", "");
map.put("马克斯", "");
map.put("辛巴", "");
while (iterator.hasNext())
{
// 获取键-值对
Map.Entry<String, Cat> pair = iterator.next();
String key = pair.getKey(); // 键
String value = pair.getValue(); // 值
System.out.println(key + " - " + value);
}//在此编写你的代码
}
public static class Cat {
String name;
public Cat(String name) {
this.name = name;
}
@Override
public String toString() {
return name != null ? name.toUpperCase() : null;
}
}
}