Java8-03
1.单排序
properties.sort(Comparator.comparing(x -> x.distance));
list.sort(Comparator.comparing(UserInformation::getName));
//正序
list=list.stream().sorted().collect(Collectors.toList());
list.stream().sorted(Comparator.comparing(Student::getAge))
//逆序
list.stream().sorted(Comparator.reverseOrder())
list.stream().sorted(Comparator.comparing(Student::getAge).reversed())
2.双排序
timeAxisList.sort(Comparator.comparing(TimeAxisDTO::getFinancialYear).thenComparing(TimeAxisDTO::getFinancialWeek));
多字段排序:
list.sort(Comparator.comparing(UserInformation::getId).thenComparing(UserInformation::getAge));
多字段排序,指定正序还是倒序:
/**
* 按照推荐、置顶、发布时间来排序
* @param list
*/
private static void sort(List<Article> list){
List<Article> sortList = list.stream() .sorted(Comparator.comparing(Article::getRecommend,Comparator.reverseOrder())
.thenComparing(Article::getTop,Comparator.reverseOrder())
.thenComparing(Article::getReleaseTime,Comparator.reverseOrder()))
.collect(Collectors.toList());
sortList.stream().forEach(System.out::println);
}
3.自定义排序
List<String> sortListA = Arrays.asList("2023", "2022", "2021", "2020", "2019", "2018", "2017");
List<String> sortListB = Arrays.asList("春", "夏", "秋", "冬");
value = value.stream().sorted(Comparator.comparing(TotalListRegionSeasonDTO::getRegionNo, Comparator.comparing(sortListA::indexOf))
.thenComparing(TotalListRegionSeasonDTO::getSeasonName, Comparator.comparing(sortListB::indexOf))).collect(Collectors.toList());
4.反转排序
salListsTopN.sort(Comparator.comparing(SalList::getSalQty).reversed());
5.中文拼音排序
List<String> sortList = Arrays.asList("春", "夏", "秋", "冬");
Collator collator = Collator.getInstance(Locale.CHINA);
styleCategoryDim = styleCategoryDim.stream().sorted(Comparator.comparing(StyleCategoryDataDTO::getSeasonName, Comparator.comparing(sortList::indexOf))
.thenComparing(StyleCategoryDataDTO::getStyleCategoryName, collator)).collect(Collectors.toList());
6.list 嵌套排序
List<List<String>>排序
/**
* List<List<String>> 排序算法
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/12 11:22
*/
public class Java8_09_stream_sort {
private static List<List<String>> METHOD_LIST_WITH_ORDER = new LinkedList<>();
/**
* 按第n位降序排列
*
* @param compareIndex
*/
private static void doSortDesc(int compareIndex) {
for (int i = 0; i < METHOD_LIST_WITH_ORDER.size() - 1; i++) {
int preIndex = i;
int currentIndex = i + 1;
Long currentItem = Long.valueOf(METHOD_LIST_WITH_ORDER.get(currentIndex).get(compareIndex));
List<String> currentObject = METHOD_LIST_WITH_ORDER.get(currentIndex);
//当前节点的值大于前一节点,交换,且是循环比较
while (preIndex >= 0 && currentItem > Long.valueOf(METHOD_LIST_WITH_ORDER.get(preIndex).get(compareIndex))) {
METHOD_LIST_WITH_ORDER.set(preIndex + 1, METHOD_LIST_WITH_ORDER.get(preIndex));
preIndex--;
}
//设置preIndex + 1的值
METHOD_LIST_WITH_ORDER.set(preIndex + 1, currentObject);
}
}
public static void main(String[] args) {
METHOD_LIST_WITH_ORDER.add(Arrays.asList("1", "3", "4", "5"));
METHOD_LIST_WITH_ORDER.add(Arrays.asList("1", "3", "4", "4"));
METHOD_LIST_WITH_ORDER.add(Arrays.asList("1", "3", "4", "7"));
METHOD_LIST_WITH_ORDER.add(Arrays.asList("1", "3", "4", "1"));
METHOD_LIST_WITH_ORDER.add(Arrays.asList("2", "3", "4", "9"));
doSortDesc(3);
for (List<String> list : METHOD_LIST_WITH_ORDER) {
for (String s : list) {
System.out.print(s + ",");
}
System.out.println();
}
System.out.println(JSON.toJSONString(METHOD_LIST_WITH_ORDER));
}
}
7.多字段升降不定
final String sortName = pageQuery.getSortname();
final String sortOrd = pageQuery.getSortord();
tags = tags.stream().sorted(
("sal_qty".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getSalQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total7_sal_qty".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotal7SalQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total14_sal_qty".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotal14SalQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total28_sal_qty".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotal28SalQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total_sal_qty".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotalSalQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total_size_store_day".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotalSizeStoreDay, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"counter_date".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getCounterDate, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"inv_store_count".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getInvStoreCount, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total_sal_qty_store_rate".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotalSalQtyStoreRate, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"total30_sal_qty_store_rate".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getTotal30SalQtyStoreRate, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
"replenish_not_arrive_qty".equals(sortName) ? Comparator.comparing(ProductAllexinfoV1DTO::getReplenishNotArriveQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()) :
Comparator.comparing(ProductAllexinfoV1DTO::getSalQty, StringUtils.equals(sortOrd, "desc") ? Comparator.reverseOrder() : Comparator.naturalOrder()))
).collect(Collectors.toList());
8.反射排序
public static void main(String[] args) {
final String sortName = "counterDate";
// final String sortOrd = "desc";
final String sortOrd = "asc";
List<ProductAllexinfoV1DTO> tags = new ArrayList<>();
ProductAllexinfoV1DTO do1 = new ProductAllexinfoV1DTO();
do1.setSalQty(26);
do1.setCounterDate("2023-01-01");
tags.add(do1);
ProductAllexinfoV1DTO do2 = new ProductAllexinfoV1DTO();
do2.setSalQty(233);
do2.setCounterDate(null);
tags.add(do2);
ProductAllexinfoV1DTO do3 = new ProductAllexinfoV1DTO();
do3.setSalQty(56);
do3.setCounterDate("2023-03-01");
tags.add(do3);
tags.sort((o1, o2) -> {
final Object object1 = Reflect.on(o1).field(sortName).get();
final Object object2 = Reflect.on(o2).field(sortName).get();
if (Objects.isNull(object1) && Objects.isNull(object2)) return 0;
if (Objects.isNull(object1)) return 1;
if (Objects.isNull(object2)) return -1;
if (object1 instanceof String) {
return StringUtils.equals(sortOrd, "desc") ? Comparator.<String>reverseOrder().compare((String) object1, (String) object2) :
Comparator.<String>naturalOrder().compare((String) object1, (String) object2);
} else if (object1 instanceof Integer) {
return StringUtils.equals(sortOrd, "desc") ? Comparator.<Integer>reverseOrder().compare((Integer) object1, (Integer) object2) :
Comparator.<Integer>naturalOrder().compare((Integer) object1, (Integer) object2);
} else if (object1 instanceof BigDecimal) {
return StringUtils.equals(sortOrd, "desc") ? Comparator.<BigDecimal>reverseOrder().compare((BigDecimal) object1, (BigDecimal) object2) :
Comparator.<BigDecimal>naturalOrder().compare((BigDecimal) object1, (BigDecimal) object2);
}
return 0;
});
System.out.println(JSONArray.toJSONString(tags));
}
测试filter:
public static void main(String[] args) {
final String sortName = "counterDate";
// final String sortOrd = "desc";
final String sortOrd = "asc";
List<ProductAllexinfoV1DTO> tags = new ArrayList<>();
ProductAllexinfoV1DTO do1 = new ProductAllexinfoV1DTO();
do1.setSalQty(26);
do1.setCounterDate("2023-01-01");
do1.setReplenishQty(111);
tags.add(do1);
ProductAllexinfoV1DTO do2 = new ProductAllexinfoV1DTO();
do2.setSalQty(233);
do2.setCounterDate(null);
do2.setReplenishQty(0);
tags.add(do2);
ProductAllexinfoV1DTO do3 = new ProductAllexinfoV1DTO();
do3.setSalQty(56);
do3.setCounterDate("2023-03-01");
do3.setReplenishQty(131);
tags.add(do3);
final Set<String> headerFilterList = new HashSet<>();
headerFilterList.add("replenishQty");
if (CollectionUtils.isNotEmpty(headerFilterList) && !headerFilterList.contains(CommonConstant.ALL)) {
for (String item : headerFilterList) {
if (StringUtils.equals("replenishQty", item) || StringUtils.equals("orderNotArriveQty", item) || StringUtils.equals("replenishNotArriveQty", item)) {
tags.stream().filter(t -> Objects.nonNull(Reflect.on(t).field(item).get()) && (Integer) Reflect.on(t).field(item).get() != 0).collect(Collectors.toList());
} else {
tags.stream().filter(t -> Objects.nonNull(Reflect.on(t).field(item).get()) && (Integer) Reflect.on(t).field(item).get() > 0).collect(Collectors.toList());
}
}
}
System.out.println(JSONArray.toJSONString(tags));
}
9.不用 stream 排序
// 正序
list.sort(Comparator.comparing(Integer::intValue));
// 倒序
list.sort(Comparator.comparing(Integer::intValue).reversed());
// 正序
list.sort(Comparator.comparing(Student::getAge));
// 倒序
list.sort(Comparator.comparing(Student::getAge).reversed());
10.含空值排序
list=list.stream().sorted(Comparator.comparing(l -> l.getCreateTime(), Comparator.nullsFirst(Date::compareTo))).collect(Collectors.toList());
List<Map<String, Object>> collect = maps.stream() .sorted(Comparator.comparing((Map<String, Object> o) -> (Double) o.get("score"),
Comparator.nullsLast(Comparator.reverseOrder()))
.thenComparing((Map<String, Object> o) -> ((double) o.get("dealerDistance")),
Comparator.nullsLast(Comparator.naturalOrder())))
.collect(Collectors.toList());
11.nullsLast
以 nullsLast()为例,项目逻辑中 dealerDistance 为 null 时排最后
Comparator.nullsLast(Comparator.reverseOrder())把 null 排序最后面,然后是 Comparator.reverseOrder(),null 值不参与逆序,null 还是在最后
Comparator.nullsLast(Double ::compareTo).reversed() 先是把 null 排最后面,再整体 reversed(),包括为 null 的,所以 null 在最前面
注:reverseOrder()是逆转排序即逆序,而 reversed()是逆转仔细理解,这两个在意思上还是有区别
12.根据 map 的 key 排序
要根据resultsMap
的键值对中的键(String
类型)进行降序排列,可以使用Comparator
接口来实现自定义比较器,并将其传递给TreeMap
类的构造函数。以下是一种实现方法:
public class Main {
public static void main(String[] args) {
// 创建一个示例的 Map
Map<String, List<StoreSalCalendarDTO>> resultsMap = new HashMap<>();
// 添加一些键值对
resultsMap.put("Key1", new ArrayList<>());
resultsMap.put("Key3", new ArrayList<>());
resultsMap.put("Key2", new ArrayList<>());
// 使用自定义比较器对键进行降序排列
Map<String, List<StoreSalCalendarDTO>> sortedMap = new TreeMap<>(new KeyComparator());
sortedMap.putAll(resultsMap);
// 打印排好序的 Map
for (Map.Entry<String, List<StoreSalCalendarDTO>> entry : sortedMap.entrySet()) {
System.out.println(entry.getKey());
}
}
static class KeyComparator implements Comparator<String> {
@Override
public int compare(String key1, String key2) {
// 降序排列
return key2.compareTo(key1);
}
}
}
在上述示例中,我们首先创建了一个示例的resultsMap
,其中包含一些键值对。然后,我们定义了一个名为KeyComparator
的内部类,实现了Comparator<String>
接口,用于比较键的值。在compare
方法中,我们通过使用key2.compareTo(key1)
实现了降序排列。最后,我们创建了一个新的TreeMap
实例,并传递了KeyComparator
对象作为参数。通过putAll
方法将原始的resultsMap
中的键值对放入新的sortedMap
中,以得到降序排列的结果。
在打印循环中,我们遍历排好序的sortedMap
的键,并将它们输出到控制台。
13.set 排序
storeSkuSizeInvTotalSalDTO.setSizeCodeList(new TreeSet<>(sizeCodes));
14.比较器特性
特殊错误:比较方法违反其一般合同
在 JDK7 版本以上,Comparator 要满足自反性,传递性,对称性,不然 Arrays.sort,Collections.sort
会报 IllegalArgumentException 异常。
- 自反性:当 两个相同的元素相比时,compare 必须返回 0,也就是 compare(o1, o1) = 0;
- 反对称性:如果 compare(o1,o2) = 1,则 compare(o2, o1)必须返回符号相反的值也就是 -1;
- 传递性:如果 a>b, b>c, 则 a 必然大于 c。也就是 compare(a,b)>0, compare(b,c)>0, 则 compare(a,c)>0
final Object object1 = Reflect.on(o1).field(sortName).get();
final Object object2 = Reflect.on(o2).field(sortName).get();
if (Objects.isNull(object1) && Objects.isNull(object2)) return 0;
if (Objects.isNull(object1)) return 1;
if (Objects.isNull(object2)) return -1;
- 点赞
- 收藏
- 关注作者
评论(0)