How to sort my Java list alphabetically?

Asked

Viewed 169 times

0

I have a Spring Framework project, and I need to alphabetize my list, and I have no idea how to do it. At first I managed to load the list, but it has to have that order. Please, how should I do?

@GET
@Path("/tipos-debitos")
@Produces(MediaType.APPLICATION_JSON)
@Permission(grupo = {Grupo.ADMINISTRADOR})
public Response listarDebitosMultas() {
    final TipoDebitoMultaTO to = new TipoDebitoMultaTO();
    try {
        Arrays.asList(TipoDebitoMulta.values()).forEach(tipoDebito -> {

            final br.gov.pe.tce.spj.data.transfer.TipoDebitoMulta dataTransfer =  new br.gov.pe.tce.spj.data.transfer.TipoDebitoMulta();

            dataTransfer.setId(tipoDebito.getCodigo());
            dataTransfer.setNome(tipoDebito.getDescricao());

            to.adicionar(dataTransfer);

        });
        final Header retorno = this.restMessageHelp.montarBaseHeader("", TipoMessage.SUCCESS);

        to.setHeader(retorno);
    } catch (final Exception e) {
        LOGGER.error(e.getLocalizedMessage());
        return Response.serverError().build();
    }
    return Response.ok(to, MediaType.APPLICATION_JSON).build();
}

lista

The list is being loaded into the variable TipoDebitoMulta.values().

I know there is the use lambda Java 8 called, I tried to use so:

Collections.sort(TipoDebitoMulta.values());

But I was unsuccessful and made a mistake.

1 answer

1


Try this:

@GET
@Path("/tipos-debitos")
@Produces(MediaType.APPLICATION_JSON)
@Permission(grupo = {Grupo.ADMINISTRADOR})
public Response listarDebitosMultas() {
    final TipoDebitoMultaTO to = new TipoDebitoMultaTO();
    try {
        Stream.of(TipoDebitoMulta.values()).map(tipoDebito -> {

            final br.gov.pe.tce.spj.data.transfer.TipoDebitoMulta dataTransfer = new br.gov.pe.tce.spj.data.transfer.TipoDebitoMulta();

            dataTransfer.setId(tipoDebito.getCodigo());
            dataTransfer.setNome(tipoDebito.getDescricao());

            return dataTransfer;
        }).sorted((a, b) -> a.getNome().compareTo(b.getNome())).forEach(to::adicionar);

        final Header retorno = this.restMessageHelp.montarBaseHeader("", TipoMessage.SUCCESS);

        to.setHeader(retorno);
    } catch (final Exception e) {
        LOGGER.error(e.getLocalizedMessage());
        return Response.serverError().build();
    }
    return Response.ok(to, MediaType.APPLICATION_JSON).build();
}

The Stream.of(...) creates a Stream from the array. The .map(...) converts a Stream<TipoDebitoMulta> in a Stream<br.gov.pe.tce.spj.data.transfer.TipoDebitoMulta> when applying the lambda to convert one into the other. The .sorted(...) orders the elements of the latter Stream using a lambda as a criterion to do this. And finally the .forEach(...) decides what will be done with each element of the Stream resultant.

  • Thank you so much, ball show, it worked.... you saved my life!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.