项目功能需求
2023-12-22 10:48:52
需求分解做法记录
功能:先按名称分组然后,组内从低到高排序,然后每组排序
做法一:
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<Item> items = Arrays.asList(
new Item("A", 3),
new Item("B", 2),
new Item("A", 1),
new Item("B", 4),
new Item("C", 1)
);
// 按名称分组,然后组内从低到高排序
Map<String, List<Item>> 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<Item> result = groupedAndSorted.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.toList());
System.out.println(result);
}
文章来源:https://blog.csdn.net/qq_43518425/article/details/135145535
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!