list集合分组:根据对象某一属性分组

 public static void main(String[] args) {
//        judeDirExists(new File("/Users/apple/Desktop/hebaoFile/funding/temporary/"));
//        System.out.println(FileUtil.getFileSuffix("a.v.c"));
        int max = 5;
//        List list = Arrays.asList("123", "12345", "1234", "4321", "1234567", "5678");
//        continueForeachJava8(max, list);
        List list = new ArrayList<>();
        FileDataVo fileDataVo = new FileDataVo();
        fileDataVo.setContent("aaa");
        fileDataVo.setFileName("funding1");
        FileDataVo fileDataVo1 = new FileDataVo();
        fileDataVo1.setContent("bbb");
        fileDataVo1.setFileName("funding2");
        FileDataVo fileDataVo2 = new FileDataVo();
        fileDataVo2.setContent("ccc");
        fileDataVo2.setFileName("funding1");
        FileDataVo fileDataVo3 = new FileDataVo();
        fileDataVo3.setContent("ddd");
        fileDataVo3.setFileName("funding2");
        FileDataVo fileDataVo4 = new FileDataVo();
        fileDataVo4.setContent("eeee");
        fileDataVo4.setFileName("funding3");
        list.add(fileDataVo);
        list.add(fileDataVo1);
        list.add(fileDataVo2);
        list.add(fileDataVo3);
        list.add(fileDataVo4);
        Map> a = listGroup2MapTwo(list);
        System.out.println(a);

    }

    private static Map> listGroup2MapTwo(List list) {
        Map> userSignSerialMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(list)) {
            // 分组
            for (FileDataVo vo : list) {
                String transDate = vo.getFileName();
                List tempList = userSignSerialMap.get(transDate);
                /*如果取不到数据,那么直接new一个空的ArrayList**/
                if (tempList == null) {
                    tempList = Lists.newArrayList();
                    tempList.add(vo);
                    userSignSerialMap.put(transDate, tempList);
                }
                else {
                    /*某个vo之前已经存放过了,则直接追加数据到原来的List里**/
                    tempList.add(vo);
                }
            }
        }
        return userSignSerialMap;
    }

你可能感兴趣的:(java)