Transforming Collections into Stream

Asked

Viewed 68 times

1

There would be ways to transform the following code to java 8 Stream?

    setores = new SetorBusiness().buscarEtiquetaFuncionario(null, codDiretoriaSelecionada, codCoordSelecionada, uf, cidade, servidor, null, null, agencia);
    parametrosRelatorio = new ParametrosRelatorio("QtdUorgFunc", SaidaRelatorioEnum.buscaEnum(tipo));
    parametrosRelatorio.getParams().put("sub", FacesContext.getCurrentInstance().getExternalContext().getRealPath("/relatorios/SubQtdUorgFunc"));
    if (!setores.isEmpty()) {
    List<Setor> listaSetores = new ArrayList<Setor>();
    Setor set;
    Map<String, List<Setor>> lista = new LinkedHashMap<String, List<Setor>>();

    for (Setor s : setores) {
        if (lista.containsKey(s.getSiglaSetor())) {
            lista.get(s.getSiglaSetor()).add(s);

        } else {
            List<Setor> se = new ArrayList<Setor>();
            se.add(s);
            lista.put(s.getSiglaSetor(), se);

        }
    }

    for (List<Setor> l : lista.values()) {
        set = new Setor();
        set.setSiglaSetor(l.get(0).getSiglaSetor());
        set.setCodigo(l.get(0).getCodigo());

        set.getSetorImpressaoPDF().addAll(l);
        listaSetores.add(set);
    }

   Util.gerarRelatorio(listaSetores, parametrosRelatorio);
  • A Setormay have several other sectors within the setorImpressaoPDF? This is strange. If the father sector and the children sector are different things, the situation becomes simpler.

  • That’s right.Victor A Sector can have several other sectors within the industry. at the end I am grouped with the code and siglasetor and a list . It is precisely for report.

1 answer

0


That should do it from here:

Map<String, List<Setor>> map = setores
        .stream()
        .collect(Collectors.groupingBy(Setor::getSiglaSetor));

List<Setor> listaSetores = map.values().stream().map(lista -> {
    Setor set = new Setor();
    set.setSiglaSetor(lista.get(0).getSiglaSetor());
    set.setCodigo(lista.get(0).getCodigo());
    set.getSetorImpressaoPDF().addAll(lista);
    return set;
}).collect(Collectors.toList());

Or you can use this equivalent if you want to be a little shorter:

List<Setor> listaSetores = setores
        .stream()
        .collect(Collectors.groupingBy(Setor::getSiglaSetor))
        .values()
        .stream()
        .map(lista -> {
            Setor set = new Setor();
            set.setSiglaSetor(lista.get(0).getSiglaSetor());
            set.setCodigo(lista.get(0).getCodigo());
            set.getSetorImpressaoPDF().addAll(lista);
            return set;
        }).collect(Collectors.toList());

Meanwhile:

  • What you want to assemble is a list of groups of sectors, not a list of sectors. The class Setor should have a single responsibility, which is to describe what a sector is in its field of application. However, its class Setor is with a second responsibility, which is to group sectors of the same code for printing the PDF. Therefore, it is best to have a class Grupo so that each class has only one responsibility.

  • The name of the method getSiglaSetor() is redundant, because once he’s in the class Setor, the suffix Setor in the name is unnecessary and getSigla() would be enough.

So with this new class, I think this would be better:

public class Grupo {
    private final String sigla;
    private final String codigo;
    private final List<Setor> setores;

    public Grupo(List<Setor> setores) {
         this.sigla = setores.get(0).getSigla();
         this.codigo = setores.get(0).getCodigo();
         this.setores = setores;
    }

    public String getSigla() {
        return sigla;
    }

    public String getCodigo() {
        return codigo;
    }

    public List<Setor> getSetorImpressaoPDF() {
        return setores;
    }
}

The code with the stream would look like this:

List<Grupo> listaGrupos = setores
        .stream()
        .collect(Collectors.groupingBy(Setor::getSigla))
        .values()
        .stream()
        .map(Grupo::new)
        .collect(Collectors.toList());

If you want to keep the original order, you can change this:

        .collect(Collectors.groupingBy(Setor::getSigla))

That’s why:

        .collect(Collectors.groupingBy(
            Setor::getSigla,
            LinkedHashMap::new,
            Collectors.toList()
        ))
  • It worked but the ordering did not come the same. How can I do with Linkedhashmap on groupingBy?

  • I think with Linkedhashmap will maintain the sort that came from the list

  • @user2509556 Edited response.

Browser other questions tagged

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