**
集合的转换-Map转换为List
**
public EchartHistogram getAgePie(StatParamBo data) throws Exception {
String userInfoPieTitle = "用户年龄占比";
//初始化各年龄段的人数和年龄段
Integer value0 = 0, value1 = 0, value2 = 0, value3 = 0, value4 = 0;
String name0 = "0-20岁", name1 = "20-30岁", name2 = "30-40岁", name3 = "40-50岁", name4 = "50岁以上";
Map<String, Integer> map = new TreeMap<String, Integer>();
//获取从数据库查询到的数据
List<SgGuiduserEntity> list = userInfoStatDao.getAgeList(data);
//定义泛型为NameValueEntity的List集合(接收处理之后的数据)
List<NameValueEntity> list1 = new ArrayList<NameValueEntity>();
//遍历身份证(获取年龄)
for (SgGuiduserEntity sg : list) {
//获取身份证号码
String idCard = sg.getCardId();
//获取年龄
Integer age = this.getIdCaidToAge(idCard);
if (age <= 20) {
value0++;
}else if (age > 20 && age <= 30){
value1++;
}else if (age > 30 && age <= 40){
value2++;
}else if (age > 40 && age <= 50){
value3++;
}else {
value4++;
}
}
/*
* 按顺序存贮数据(上面的Map是用TreeMap的形式存储,所以自带顺序)
*/
map.put(name0, value0);
map.put(name1, value1);
map.put(name2, value2);
map.put(name3, value3);
map.put(name4, value4);
/*
* 将map转换为list
*/
//使用keySet()返回map中所有key值的列表。
Set<String> listSet = map.keySet();
//遍历listSet(即:获取到的key值)
Iterator<String> iterator = listSet.iterator();
while(iterator.hasNext()) {
//将获取到的值赋给NameValueEntity类的Name和Value属性。
NameValueEntity itemEntity = new NameValueEntity();
String key = iterator.next();
Integer value = map.get(key);
itemEntity.setName(key);
itemEntity.setValue(value);
list1.add(itemEntity);
}
EchartHistogram agePie = EchartTransForm.transFormHistogramNew(userInfoPieTitle, list1);
return agePie;
}