为什么最后一条会出错呢
package zh.codegym.task.task09.task0927;
import java.util.*;
/*
十只猫
*/
public class Solution {
public static void main(String[] args) {
Map<String, Cat> map = createMap();
Set<Cat> set = convertMapToSet(map);
printCatSet(set);
}
public static Map<String, Cat> createMap() {
//在此编写你的代码
Map<String,Cat> map=new HashMap<>();
for(int i=0;i<10;i++){
map.put("mimi"+i,new Cat("mimi"+i));
}
return map;
}
public static Set<Cat> convertMapToSet(Map<String, Cat> map) {
//在此编写你的代码
/*Iterator<Map.Entry<String,Cat>> iterator=map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Cat> pair=iterator.next();
set.add(pair.getValue());
}*/
return new HashSet<>(map.values());
}
public static void printCatSet(Set<Cat> set) {
for (Cat cat : set) {
System.out.println(cat);
}
}
public static class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public String toString() {
return "猫 " + this.name;
}
}
}