首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SpringBoot框架中对bean对象进行排序?

如何在SpringBoot框架中对bean对象进行排序?
EN

Stack Overflow用户
提问于 2021-07-31 16:18:38
回答 1查看 23关注 0票数 0

我不能像下面这样在服务类(服务类)中使用sort()方法,并且它显示了类似于“询问我在'util‘包中创建集合类”的错误。

服务类别:

代码语言:javascript
复制
@Service
public class StudentManagement { 

    public List<Student> display() {
        ArrayList<Student> l=new ArrayList<Student>();
        l.add(new Student(10,"John"));//convert string object to Student 
        l.add(new Student(20,"Smith"));
        l.add(new Student(5,"Beck"));
        l.add(new Student(15 ,"Joe"));
        Collections.sort(l);  
        return l;
    }
}

下面是一个控制器类

代码语言:javascript
复制
@RequestMapping(value="/hello")
public List<Student> show() { 
    return s.display();
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-01 07:05:19

我会使用java stream api (你需要java 8 +)和

代码语言:javascript
复制
return l.stream()
    .sorted(Comparator.comparing(Student::name)) // name or whatever you want to sort
    .collect(Collectors.toList());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68603849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档