您的位置:首页 > 其它

将list中的元素按照属性分类成树状的map

2017-02-07 10:50 281 查看
技术交流群: 233513714

public LinkedHashMap<String, List<TPhoneModel>> queryPhoneList(List<TPhoneModel> list) {
LinkedHashMap<String, List<TPhoneModel>> map = new LinkedHashMap<>();
for (TPhoneModel li : list) {
        //将需要归类的属性与map中的key进行比较,如果map中有该key则添加bean如果没有则新增key
if (map.containsKey(li.getAnotherName())) {
        //取出map中key对应的list并将遍历出的bean放入该key对应的list中
ArrayList<TPhoneModel> templist = (ArrayList<TPhoneModel>) map.get(li.getAnotherName());
templist.add(li);
} else {
        //创建新的list
ArrayList<TPhoneModel> temlist = new ArrayList<TPhoneModel>();
temlist.add(li);
map.put(li.getAnotherName(), temlist);
}
}
return map;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: