0

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?

ceng
  • 128
  • 3
  • 17

1 Answers1

1

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));
I_AM__PAUL
  • 118
  • 7