当前位置: 代码网 > it编程>编程语言>Java > Java中Stream实现List排序的六个核心技巧总结

Java中Stream实现List排序的六个核心技巧总结

2025年04月24日 Java 我要评论
一、基础排序实现1.1 自然序排序(正序)list<entity> sortedlist = originallist.stream() .sorted(comparator

一、基础排序实现

1.1 自然序排序(正序)

list<entity> sortedlist = originallist.stream()
        .sorted(comparator.comparing(entity::getid))
        .collect(collectors.tolist());

1.2 反向排序(倒序)

list<entity> sortedlist = originallist.stream()
        .sorted(comparator.comparing(entity::getid).reversed())
        .collect(collectors.tolist());

二、进阶排序技巧

2.1 空值安全处理

// 处理可能为null的字段
comparator<entity> nullsafecomparator = comparator.comparing(
    entity::getid, 
    comparator.nullsfirst(comparator.naturalorder())
);

list<entity> sortedlist = originallist.stream()
        .sorted(nullsafecomparator)
        .collect(collectors.tolist());

2.2 多字段组合排序

list<entity> sortedlist = originallist.stream()
        .sorted(comparator.comparing(entity::getdepartment)
                .thencomparing(entity::getid))
        .collect(collectors.tolist());

三、性能优化建议

3.1 并行流加速(适用于大数据量)

list<entity> sortedlist = originallist.parallelstream()
        .sorted(comparator.comparing(entity::getid))
        .collect(collectors.tolist());

3.2 原地排序(修改原集合)

originallist.sort(comparator.comparing(entity::getid));

四、最佳实践

  • 类型明确化:推荐指定具体集合类型
arraylist<entity> sortedlist = originallist.stream()
        .sorted(comparator.comparing(entity::getid))
        .collect(collectors.tocollection(arraylist::new));
  • 防御性拷贝:保持原集合不可变
list<entity> sortedlist = new arraylist<>(originallist);
sortedlist.sort(comparator.comparing(entity::getid));
  • lambda优化:复杂场景使用lambda表达式
list<entity> sortedlist = originallist.stream()
        .sorted((e1, e2) -> {
            // 自定义比较逻辑
            return e1.getid().compareto(e2.getid());
        })
        .collect(collectors.tolist());

五、注意事项

  • 不可变性collectors.tolist()返回的list实现可能不支持修改
  • 空指针防护:推荐始终使用comparator.nullsfirst/nullslast
  • 性能权衡:超过10万条数据时优先考虑传统排序方式
  • 对象状态:stream操作不会修改原始集合元素

六、完整示例

public class sortingdemo {
    public static void main(string[] args) {
        list<entity> entities = arrays.aslist(
            new entity(2, "b"),
            new entity(1, "a"),
            new entity(3, "c")
        );

        // 多条件排序:先按名称倒序,再按id正序
        list<entity> sorted = entities.stream()
                .sorted(comparator.comparing(entity::getname)
                        .reversed()
                        .thencomparing(entity::getid))
                .collect(collectors.tolist());

        sorted.foreach(system.out::println);
    }
}

class entity {
    private int id;
    private string name;
    
    // 构造方法和getter省略
}

七、总结对比

排序方式时间复杂度空间复杂度适用场景
stream顺序流o(n log n)o(n)通用场景
stream并行流o(n log n)o(n)大数据量(10w+)
collections.sorto(n log n)o(1)原地修改需求
数据库排序o(n log n)o(1)数据源在数据库时

通过合理选择排序策略,可以在保证代码简洁性的同时兼顾系统性能。建议根据实际业务场景选择最合适的排序方式。

到此这篇关于java中stream实现list排序的六个核心技巧的文章就介绍到这了,更多相关java stream实现list排序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com