英文:
How to sort a list of field error and a set of ConstraintViolation?
问题
你好,我有以下问题,我不是很理解,我一直在学习Java,但仍然遇到问题。
我想排序:
Set<ConstraintViolation<?>> ex = ex.getConstraintViolations();
Collections.sort(ex, Comparator.comparing(ConstraintViolation::getMessageTemplate));
我成功地得到了所有的错误,但我想逐个显示,因为当前的设置是无序的。
同时,我也未能对FieldError列表进行排序:
List<FieldError> err = ex.getFieldErrors();
Collections.sort(err);
我收到以下错误:
类型Collections中的sort(List<T>)方法不适用于参数(List<FieldError>)
我应该怎么做才能对这些列表进行排序,按照DefaultMessage或MessageTemplate的顺序只显示一个错误?
英文:
Hello I have the following problem that I do not understand very well, I have been introducing myself to java but I still have problems
I want to order
Set<ConstraintViolation<?>> ex= ex.getConstraintViolations();
Collections.sort(ex, Comparator.comparing(ConstraintViolation::getMessageTemplate));
what I manage to achieve is to have all the errors, but I want to show one by one since currently without ordering how is a set and is not ordered
also failed to sort a list of fielderror
List<FieldError> err= ex.getFieldErrors();
Collections.sort(err);
I get the following error
The method sort(List<T>) in the type Collections is not applicable for the arguments (List<FieldError>)
What do I have to do to sort these lists and show the DefaultMessage or the MessageTemplate ordered and only an error?
答案1
得分: 1
希望这有所帮助。
尝试使用Java Lambda API按字段错误进行排序:
fieldErrors.stream().sorted().collect(Collectors.toList());
对于第二个,请尝试使用以下方式:
ex.stream().sorted(Comparator.comparing(ConstraintViolation::getMessageTemplate)).collect(Collectors.toCollection(LinkedHashSet::new));
英文:
i am not on my computer to test this but i hope this helps.
Try to sort them by Java Lambda API.
fieldErrors.stream().sorted().collect(Collectors.toList());
For Second one try to use this one :
ex.stream().sorted(Comparator.comparing(ConstraintViolation::getMessageTemplate)).collect(Collectors.toCollection(LinkedHashSet::new));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论