EnumMap测试用例
public class Test{
public static void main(String[] args){
Herb[] herbInGarden = Garden.GetAllHerbOfGarden();
Map<Herb.Type,Set<Herb>> herbByType = new EnumMap<Herb.Type,Set<Herb>>;
for(Herb.Type herbType : Herb.Type.values())
herbByType.put(herbByType,new Set<Herb>);//Set<Herb>集合是空的
for(Herb herb : herbInGarden)
herbByType.get(herb.Type).add(herb);
system.out.printLn(herbByType);
}
}
public class Herb{
public enum Type{ANNUAL,BIENNIAL,PERNNIAL};
private String name;
private String type;
Herb(String name,String type){
this.name = name;
this.type = type;
}
@Override
public String toString(){
return name;
}
}
public class Garden{
public static final Herb[] GetAllHerbOfGarden(){
return getHerbArray();
}
public Herb[] getHerbArray(){
List<Herb>() herbArray = new ArrayList<Herb>();
for(int herbAge = 1;herbAge<3; herbAge++){
for(int i=0,j=0;i<10;i++,j++){
String herbName = RandomStringUtils.randomAlphabetic(5);
Herb herb = new Herb(herbName,herbAge);
herbArray.add(herb);
}
return herbArray;
}
}
}