项目功能需求

需求分解做法记录
功能:先按名称分组然后,组内从低到高排序,然后每组排序

做法一:

List<CruiseDetailOutputDto> result =  new ArrayList<CruiseDetailOutputDto>();
        List<CruiseDetailOutputDto> journeyCruiseList =  new ArrayList<CruiseDetailOutputDto>();

        journeyCruiseList.add(new CruiseDetailOutputDto("*虚拟阳台双人内舱房-4U", new BigDecimal(7798)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*虚拟阳台双人内舱房-4U", new BigDecimal(7148)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*船中虚拟阳台双人内舱房-2U", new BigDecimal(7810)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*船中虚拟阳台双人内舱房-2U", new BigDecimal(7606)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*船中虚拟阳台家庭内舱四人间-1U", new BigDecimal(9814)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*船中虚拟阳台家庭内舱四人间-1U", new BigDecimal(11143)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*豪华阳台三人间-1C", new BigDecimal(14522)));
        journeyCruiseList.add(new CruiseDetailOutputDto("*豪华阳台三人间-1C", new BigDecimal(17247)));

        Map<String, List<CruiseDetailOutputDto>> resourceMap = new LinkedHashMap<String, List<CruiseDetailOutputDto>>();
        for (CruiseDetailOutputDto cruiseDetailOutputDto : journeyCruiseList) {
            if (!resourceMap.containsKey(cruiseDetailOutputDto.getRoomName())) {
                resourceMap.put(cruiseDetailOutputDto.getRoomName(), new ArrayList<CruiseDetailOutputDto>());
            }
            resourceMap.get(cruiseDetailOutputDto.getRoomName()).add(cruiseDetailOutputDto);
        }

        List<List<CruiseDetailOutputDto>> groupsSort = new ArrayList<List<CruiseDetailOutputDto>>(resourceMap.values());
        for (List<CruiseDetailOutputDto> cruiseDetailOutputDtos : groupsSort) {
            Collections.sort(cruiseDetailOutputDtos, new Comparator<CruiseDetailOutputDto>() {
                public int compare(CruiseDetailOutputDto a, CruiseDetailOutputDto b) {
                    return a.getMainResPrice().compareTo(b.getMainResPrice());
                }
            });
        }

        // 2.按分组中的最低价格重新排序分组
        List<List<CruiseDetailOutputDto>> groups = new ArrayList<List<CruiseDetailOutputDto>>(resourceMap.values());
        Collections.sort(groups, new Comparator<List<CruiseDetailOutputDto>>() {
            public int compare(List<CruiseDetailOutputDto> a, List<CruiseDetailOutputDto> b) {
                return a.get(0).getMainResPrice().compareTo(b.get(0).getMainResPrice());
            }
        });

        for (List<CruiseDetailOutputDto> group : groups) {
            for (CruiseDetailOutputDto cruiseDetailOutputDto : group) {
                result.add(cruiseDetailOutputDto);
                System.out.println(cruiseDetailOutputDto);
            }
        }

做法二

public static void main(String[] args) {
        List items = Arrays.asList(
                new Item("A", 3),
                new Item("B", 2),
                new Item("A", 1),
                new Item("B", 4),
                new Item("C", 1)
        );

        // 按名称分组,然后组内从低到高排序
        Map> groupedAndSorted = items.stream()
                .collect(Collectors.groupingBy(Item::getName,
                        Collectors.mapping(item -> item, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> entry.getValue().stream()
                                .sorted((a, b) -> a.getValue() - b.getValue())
                                .collect(Collectors.toList())));

        // 每组排序
        List result = groupedAndSorted.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .flatMap(entry -> entry.getValue().stream())
                .collect(Collectors.toList());

        System.out.println(result);
    }

你可能感兴趣的:(windows)