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

技术交流群: 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; }
原文地址:https://www.cnblogs.com/cnndevelop/p/6373097.html