如何对字段错误列表和一组ConstraintViolation进行排序?

huangapple go评论77阅读模式
英文:

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&lt;ConstraintViolation&lt;?&gt;&gt; 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&lt;FieldError&gt; err= ex.getFieldErrors();
			Collections.sort(err);

I get the following error

The method sort(List&lt;T&gt;) in the type Collections is not applicable for the arguments (List&lt;FieldError&gt;)

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));

huangapple
  • 本文由 发表于 2020年9月14日 21:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63885470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定