Generate String from list contents

Asked

Viewed 86 times

2

I have a class called CoreFiltroSearch that has a list of ICoreFiltro calling for filtros. Follows the class and interface:

public class CoreFiltroSearch {

    private List<ICoreFiltro> filtros = new ArrayList<>();
    private String            order   = "asc";
    private String            sort    = "id";
    private Integer           page    = 0;
    private Integer           size    = 40;
}

public interface ICoreFiltro {

    String getChave();

    String getValor();

    Boolean isQFilter();

    Boolean isDateFilter();

}

From the list filtros, need to create a String using the fields getChave() and getvalor() who are of the element ICoreFiltro.

I tried using a solution I found on a blog, but I can’t even upload the server using this solution, put the question here, the solution I used in an attempt to create this String was that:

private String montarQueryString(CoreFiltroSearch filter) {
    return filter.getFiltros().stream()
            .map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))
            .reduce((p1, p2) -> p1 + "&" + p2)
            .map(s -> "?" + s).orElse("");
}

static String urlEncodeUTF8(String s) {
    try {
        return URLEncoder.encode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new UnsupportedOperationException(e);
    }
}

If anyone can help me with a better solution I appreciate, but I think there’s no problem with that logic.

  • I did it last week, but without "reducing". I just peed and collected at the end. I will pass the project link later explain in a reply

  • 1

    https://gitlab.com/geosales-open-source/tc-http-conn/blob/master/src/main/java/br/com/softsite/httpconn/HttpConn.java in the method serializeFormContent. I’m having difficulty sending the link of the exact line because the smartphone browser does not help, but it is in this method that I do the magic.

1 answer

1


We went through a similar situation. How to generate the queryParam from a set of parameterizations? More specifically, from a set of keys and values?

As I mentioned in the comment, I went through this need on a project company open.

The idea of starting from a key/value list we had very similar, so it’s a good start. I assumed in my project that there may be parameters of query that are simply indicated by the presence, so I treated the possibility of the "value" associated with the "key" being null. But that does not seem to be your case. I will assume that they are always filled.

So we mapped this stream key/value in a stream string. So far we agree perfectly:

filter.getFiltros().stream()
    .map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))

After that, we now need to turn this into a big string. As I had to know much of the library stream from Java 8 to support in Totalcross to something similar, I’ve already imagined this to be a collection using the collector joining.

There are 3 variations of joining:

  1. the one that does not receive arguments, the output is the strings on the other side of the other
  2. the one that takes an argument, which delimits the o-separator-between-the-strings-of-stream
  3. the one that takes 3 arguments, similar to the previous variation, but adding a sequence of characters at the beginning and another at the end

As we do not want to change anything neither at the beginning nor at the end, but switch these key/value pairs with "&", then we should use the collect(Collectors.joining("&")). Thus remaining the code:

filter.getFiltros().stream()
    .map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))
    .collect(Collectors.joining("&"));

If you want to go a little deeper into the issues of reducing/collecting, I remember these two questions here at Sopt that deal with this:

Browser other questions tagged

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