我不能像下面这样在服务类(服务类)中使用sort()方法,并且它显示了类似于“询问我在'util‘包中创建集合类”的错误。
服务类别:
@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;
}
}下面是一个控制器类
@RequestMapping(value="/hello")
public List<Student> show() {
return s.display();
}发布于 2021-08-01 07:05:19
我会使用java stream api (你需要java 8 +)和
return l.stream()
.sorted(Comparator.comparing(Student::name)) // name or whatever you want to sort
.collect(Collectors.toList());https://stackoverflow.com/questions/68603849
复制相似问题